github.com/aliyun/aliyun-oss-go-sdk@v3.0.2+incompatible/sample/The Go Programming Language.html (about) 1 <!DOCTYPE html> 2 <!-- saved from url=(0032)https://golang.org/pkg/net/http/ --> 3 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4 5 <meta name="viewport" content="width=device-width, initial-scale=1"> 6 <meta name="theme-color" content="#375EAB"> 7 8 <title>http - The Go Programming Language</title> 9 10 <link type="text/css" rel="stylesheet" href="./http - The Go Programming Language_files/style.css"> 11 12 <link rel="search" type="application/opensearchdescription+xml" title="godoc" href="https://golang.org/opensearch.xml"> 13 14 <link rel="stylesheet" href="./http - The Go Programming Language_files/jquery.treeview.css"> 15 <script type="text/javascript" async="" src="./http - The Go Programming Language_files/ga.js"></script><script type="text/javascript">window.initFuncs = [];</script> 16 <script type="text/javascript"> 17 var _gaq = _gaq || []; 18 _gaq.push(["_setAccount", "UA-11222381-2"]); 19 _gaq.push(["b._setAccount", "UA-49880327-6"]); 20 window.trackPageview = function() { 21 _gaq.push(["_trackPageview", location.pathname+location.hash]); 22 _gaq.push(["b._trackPageview", location.pathname+location.hash]); 23 }; 24 window.trackPageview(); 25 window.trackEvent = function(category, action, opt_label, opt_value, opt_noninteraction) { 26 _gaq.push(["_trackEvent", category, action, opt_label, opt_value, opt_noninteraction]); 27 _gaq.push(["b._trackEvent", category, action, opt_label, opt_value, opt_noninteraction]); 28 }; 29 </script> 30 </head> 31 <body> 32 33 <div id="lowframe" style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;"> 34 ... 35 </div><!-- #lowframe --> 36 37 <div id="topbar" class="wide"><div class="container"> 38 <div class="top-heading" id="heading-wide"><a href="https://golang.org/">The Go Programming Language</a></div> 39 <div class="top-heading" id="heading-narrow"><a href="https://golang.org/">Go</a></div> 40 <a href="https://golang.org/pkg/net/http/#" id="menu-button"><span id="menu-button-arrow">▽</span></a> 41 <form method="GET" action="https://golang.org/search"> 42 <div id="menu" style="min-width: 60px;"> 43 <a href="https://golang.org/doc/">Documents</a> 44 <a href="https://golang.org/pkg/">Packages</a> 45 <a href="https://golang.org/project/">The Project</a> 46 <a href="https://golang.org/help/">Help</a> 47 <a href="https://golang.org/blog/">Blog</a> 48 49 <a id="playgroundButton" href="http://play.golang.org/" title="Show Go Playground" style="display: inline;">Play</a> 50 51 <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search"> 52 </div> 53 </form> 54 55 </div></div> 56 57 58 <div id="playground" class="play"> 59 <div class="input"><textarea class="code">package main 60 61 import "fmt" 62 63 func main() { 64 fmt.Println("Hello, 世界") 65 }</textarea></div> 66 <div class="output"></div> 67 <div class="buttons"> 68 <a class="run" title="Run this code [shift-enter]">Run</a> 69 <a class="fmt" title="Format this code">Format</a> 70 71 </div> 72 </div> 73 74 75 <div id="page" class="wide" tabindex="-1" style="outline: 0px;"> 76 <div class="container"> 77 78 79 <h1>Package http</h1> 80 81 82 83 84 <div id="nav"></div> 85 86 87 <!-- 88 Copyright 2009 The Go Authors. All rights reserved. 89 Use of this source code is governed by a BSD-style 90 license that can be found in the LICENSE file. 91 --> 92 <!-- 93 Note: Static (i.e., not template-generated) href and id 94 attributes start with "pkg-" to make it impossible for 95 them to conflict with generated attributes (some of which 96 correspond to Go identifiers). 97 --> 98 99 <script type="text/javascript"> 100 document.ANALYSIS_DATA = null; 101 document.CALLGRAPH = null; 102 </script> 103 104 105 106 <div id="short-nav"> 107 <dl> 108 <dd><code>import "net/http"</code></dd> 109 </dl> 110 <dl> 111 <dd><a href="https://golang.org/pkg/net/http/#pkg-overview" class="overviewLink">Overview</a></dd> 112 <dd><a href="https://golang.org/pkg/net/http/#pkg-index" class="indexLink">Index</a></dd> 113 114 <dd><a href="https://golang.org/pkg/net/http/#pkg-examples" class="examplesLink">Examples</a></dd> 115 116 117 <dd><a href="https://golang.org/pkg/net/http/#pkg-subdirectories">Subdirectories</a></dd> 118 119 </dl> 120 </div> 121 <!-- The package's Name is printed as title by the top-level template --> 122 <div id="pkg-overview" class="toggleVisible"> 123 <div class="collapsed"> 124 <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2> 125 </div> 126 <div class="expanded"> 127 <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2> 128 <p> 129 Package http provides HTTP client and server implementations. 130 </p> 131 <p> 132 Get, Head, Post, and PostForm make HTTP (or HTTPS) requests: 133 </p> 134 <pre>resp, err := http.Get("<a href="http://example.com/">http://example.com/</a>") 135 ... 136 resp, err := http.Post("<a href="http://example.com/upload">http://example.com/upload</a>", "image/jpeg", &buf) 137 ... 138 resp, err := http.PostForm("<a href="http://example.com/form">http://example.com/form</a>", 139 url.Values{"key": {"Value"}, "id": {"123"}}) 140 </pre> 141 <p> 142 The client must close the response body when finished with it: 143 </p> 144 <pre>resp, err := http.Get("<a href="http://example.com/">http://example.com/</a>") 145 if err != nil { 146 // handle error 147 } 148 defer resp.Body.Close() 149 body, err := ioutil.ReadAll(resp.Body) 150 // ... 151 </pre> 152 <p> 153 For control over HTTP client headers, redirect policy, and other 154 settings, create a Client: 155 </p> 156 <pre>client := &http.Client{ 157 CheckRedirect: redirectPolicyFunc, 158 } 159 160 resp, err := client.Get("<a href="http://example.com/">http://example.com</a>") 161 // ... 162 163 req, err := http.NewRequest("GET", "<a href="http://example.com/">http://example.com</a>", nil) 164 // ... 165 req.Header.Add("If-None-Match", `W/"wyzzy"`) 166 resp, err := client.Do(req) 167 // ... 168 </pre> 169 <p> 170 For control over proxies, TLS configuration, keep-alives, 171 compression, and other settings, create a Transport: 172 </p> 173 <pre>tr := &http.Transport{ 174 TLSClientConfig: &tls.Config{RootCAs: pool}, 175 DisableCompression: true, 176 } 177 client := &http.Client{Transport: tr} 178 resp, err := client.Get("<a href="https://example.com/">https://example.com</a>") 179 </pre> 180 <p> 181 Clients and Transports are safe for concurrent use by multiple 182 goroutines and for efficiency should only be created once and re-used. 183 </p> 184 <p> 185 ListenAndServe starts an HTTP server with a given address and handler. 186 The handler is usually nil, which means to use DefaultServeMux. 187 Handle and HandleFunc add handlers to DefaultServeMux: 188 </p> 189 <pre>http.Handle("/foo", fooHandler) 190 191 http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { 192 fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) 193 }) 194 195 log.Fatal(http.ListenAndServe(":8080", nil)) 196 </pre> 197 <p> 198 More control over the server's behavior is available by creating a 199 custom Server: 200 </p> 201 <pre>s := &http.Server{ 202 Addr: ":8080", 203 Handler: myHandler, 204 ReadTimeout: 10 * time.Second, 205 WriteTimeout: 10 * time.Second, 206 MaxHeaderBytes: 1 << 20, 207 } 208 log.Fatal(s.ListenAndServe()) 209 </pre> 210 211 </div> 212 </div> 213 214 215 <div id="pkg-index" class="toggleVisible"> 216 <div class="collapsed"> 217 <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2> 218 </div> 219 <div class="expanded"> 220 <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2> 221 222 <!-- Table of contents for API; must be named manual-nav to turn off auto nav. --> 223 <div id="manual-nav"> 224 <dl> 225 226 <dd><a href="https://golang.org/pkg/net/http/#pkg-constants">Constants</a></dd> 227 228 229 <dd><a href="https://golang.org/pkg/net/http/#pkg-variables">Variables</a></dd> 230 231 232 233 <dd><a href="https://golang.org/pkg/net/http/#CanonicalHeaderKey">func CanonicalHeaderKey(s string) string</a></dd> 234 235 236 <dd><a href="https://golang.org/pkg/net/http/#DetectContentType">func DetectContentType(data []byte) string</a></dd> 237 238 239 <dd><a href="https://golang.org/pkg/net/http/#Error">func Error(w ResponseWriter, error string, code int)</a></dd> 240 241 242 <dd><a href="https://golang.org/pkg/net/http/#Handle">func Handle(pattern string, handler Handler)</a></dd> 243 244 245 <dd><a href="https://golang.org/pkg/net/http/#HandleFunc">func HandleFunc(pattern string, handler func(ResponseWriter, *Request))</a></dd> 246 247 248 <dd><a href="https://golang.org/pkg/net/http/#ListenAndServe">func ListenAndServe(addr string, handler Handler) error</a></dd> 249 250 251 <dd><a href="https://golang.org/pkg/net/http/#ListenAndServeTLS">func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler) error</a></dd> 252 253 254 <dd><a href="https://golang.org/pkg/net/http/#MaxBytesReader">func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser</a></dd> 255 256 257 <dd><a href="https://golang.org/pkg/net/http/#NotFound">func NotFound(w ResponseWriter, r *Request)</a></dd> 258 259 260 <dd><a href="https://golang.org/pkg/net/http/#ParseHTTPVersion">func ParseHTTPVersion(vers string) (major, minor int, ok bool)</a></dd> 261 262 263 <dd><a href="https://golang.org/pkg/net/http/#ParseTime">func ParseTime(text string) (t time.Time, err error)</a></dd> 264 265 266 <dd><a href="https://golang.org/pkg/net/http/#ProxyFromEnvironment">func ProxyFromEnvironment(req *Request) (*url.URL, error)</a></dd> 267 268 269 <dd><a href="https://golang.org/pkg/net/http/#ProxyURL">func ProxyURL(fixedURL *url.URL) func(*Request) (*url.URL, error)</a></dd> 270 271 272 <dd><a href="https://golang.org/pkg/net/http/#Redirect">func Redirect(w ResponseWriter, r *Request, urlStr string, code int)</a></dd> 273 274 275 <dd><a href="https://golang.org/pkg/net/http/#Serve">func Serve(l net.Listener, handler Handler) error</a></dd> 276 277 278 <dd><a href="https://golang.org/pkg/net/http/#ServeContent">func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker)</a></dd> 279 280 281 <dd><a href="https://golang.org/pkg/net/http/#ServeFile">func ServeFile(w ResponseWriter, r *Request, name string)</a></dd> 282 283 284 <dd><a href="https://golang.org/pkg/net/http/#SetCookie">func SetCookie(w ResponseWriter, cookie *Cookie)</a></dd> 285 286 287 <dd><a href="https://golang.org/pkg/net/http/#StatusText">func StatusText(code int) string</a></dd> 288 289 290 291 <dd><a href="https://golang.org/pkg/net/http/#Client">type Client</a></dd> 292 293 294 295 <dd> <a href="https://golang.org/pkg/net/http/#Client.Do">func (c *Client) Do(req *Request) (resp *Response, err error)</a></dd> 296 297 298 <dd> <a href="https://golang.org/pkg/net/http/#Client.Get">func (c *Client) Get(url string) (resp *Response, err error)</a></dd> 299 300 301 <dd> <a href="https://golang.org/pkg/net/http/#Client.Head">func (c *Client) Head(url string) (resp *Response, err error)</a></dd> 302 303 304 <dd> <a href="https://golang.org/pkg/net/http/#Client.Post">func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *Response, err error)</a></dd> 305 306 307 <dd> <a href="https://golang.org/pkg/net/http/#Client.PostForm">func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error)</a></dd> 308 309 310 311 <dd><a href="https://golang.org/pkg/net/http/#CloseNotifier">type CloseNotifier</a></dd> 312 313 314 315 316 <dd><a href="https://golang.org/pkg/net/http/#ConnState">type ConnState</a></dd> 317 318 319 320 <dd> <a href="https://golang.org/pkg/net/http/#ConnState.String">func (c ConnState) String() string</a></dd> 321 322 323 324 <dd><a href="https://golang.org/pkg/net/http/#Cookie">type Cookie</a></dd> 325 326 327 328 <dd> <a href="https://golang.org/pkg/net/http/#Cookie.String">func (c *Cookie) String() string</a></dd> 329 330 331 332 <dd><a href="https://golang.org/pkg/net/http/#CookieJar">type CookieJar</a></dd> 333 334 335 336 337 <dd><a href="https://golang.org/pkg/net/http/#Dir">type Dir</a></dd> 338 339 340 341 <dd> <a href="https://golang.org/pkg/net/http/#Dir.Open">func (d Dir) Open(name string) (File, error)</a></dd> 342 343 344 345 <dd><a href="https://golang.org/pkg/net/http/#File">type File</a></dd> 346 347 348 349 350 <dd><a href="https://golang.org/pkg/net/http/#FileSystem">type FileSystem</a></dd> 351 352 353 354 355 <dd><a href="https://golang.org/pkg/net/http/#Flusher">type Flusher</a></dd> 356 357 358 359 360 <dd><a href="https://golang.org/pkg/net/http/#Handler">type Handler</a></dd> 361 362 363 <dd> <a href="https://golang.org/pkg/net/http/#FileServer">func FileServer(root FileSystem) Handler</a></dd> 364 365 366 <dd> <a href="https://golang.org/pkg/net/http/#NotFoundHandler">func NotFoundHandler() Handler</a></dd> 367 368 369 <dd> <a href="https://golang.org/pkg/net/http/#RedirectHandler">func RedirectHandler(url string, code int) Handler</a></dd> 370 371 372 <dd> <a href="https://golang.org/pkg/net/http/#StripPrefix">func StripPrefix(prefix string, h Handler) Handler</a></dd> 373 374 375 <dd> <a href="https://golang.org/pkg/net/http/#TimeoutHandler">func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler</a></dd> 376 377 378 379 380 <dd><a href="https://golang.org/pkg/net/http/#HandlerFunc">type HandlerFunc</a></dd> 381 382 383 384 <dd> <a href="https://golang.org/pkg/net/http/#HandlerFunc.ServeHTTP">func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)</a></dd> 385 386 387 388 <dd><a href="https://golang.org/pkg/net/http/#Header">type Header</a></dd> 389 390 391 392 <dd> <a href="https://golang.org/pkg/net/http/#Header.Add">func (h Header) Add(key, value string)</a></dd> 393 394 395 <dd> <a href="https://golang.org/pkg/net/http/#Header.Del">func (h Header) Del(key string)</a></dd> 396 397 398 <dd> <a href="https://golang.org/pkg/net/http/#Header.Get">func (h Header) Get(key string) string</a></dd> 399 400 401 <dd> <a href="https://golang.org/pkg/net/http/#Header.Set">func (h Header) Set(key, value string)</a></dd> 402 403 404 <dd> <a href="https://golang.org/pkg/net/http/#Header.Write">func (h Header) Write(w io.Writer) error</a></dd> 405 406 407 <dd> <a href="https://golang.org/pkg/net/http/#Header.WriteSubset">func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error</a></dd> 408 409 410 411 <dd><a href="https://golang.org/pkg/net/http/#Hijacker">type Hijacker</a></dd> 412 413 414 415 416 <dd><a href="https://golang.org/pkg/net/http/#ProtocolError">type ProtocolError</a></dd> 417 418 419 420 <dd> <a href="https://golang.org/pkg/net/http/#ProtocolError.Error">func (err *ProtocolError) Error() string</a></dd> 421 422 423 424 <dd><a href="https://golang.org/pkg/net/http/#Request">type Request</a></dd> 425 426 427 <dd> <a href="https://golang.org/pkg/net/http/#NewRequest">func NewRequest(method, urlStr string, body io.Reader) (*Request, error)</a></dd> 428 429 430 <dd> <a href="https://golang.org/pkg/net/http/#ReadRequest">func ReadRequest(b *bufio.Reader) (req *Request, err error)</a></dd> 431 432 433 434 <dd> <a href="https://golang.org/pkg/net/http/#Request.AddCookie">func (r *Request) AddCookie(c *Cookie)</a></dd> 435 436 437 <dd> <a href="https://golang.org/pkg/net/http/#Request.BasicAuth">func (r *Request) BasicAuth() (username, password string, ok bool)</a></dd> 438 439 440 <dd> <a href="https://golang.org/pkg/net/http/#Request.Cookie">func (r *Request) Cookie(name string) (*Cookie, error)</a></dd> 441 442 443 <dd> <a href="https://golang.org/pkg/net/http/#Request.Cookies">func (r *Request) Cookies() []*Cookie</a></dd> 444 445 446 <dd> <a href="https://golang.org/pkg/net/http/#Request.FormFile">func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error)</a></dd> 447 448 449 <dd> <a href="https://golang.org/pkg/net/http/#Request.FormValue">func (r *Request) FormValue(key string) string</a></dd> 450 451 452 <dd> <a href="https://golang.org/pkg/net/http/#Request.MultipartReader">func (r *Request) MultipartReader() (*multipart.Reader, error)</a></dd> 453 454 455 <dd> <a href="https://golang.org/pkg/net/http/#Request.ParseForm">func (r *Request) ParseForm() error</a></dd> 456 457 458 <dd> <a href="https://golang.org/pkg/net/http/#Request.ParseMultipartForm">func (r *Request) ParseMultipartForm(maxMemory int64) error</a></dd> 459 460 461 <dd> <a href="https://golang.org/pkg/net/http/#Request.PostFormValue">func (r *Request) PostFormValue(key string) string</a></dd> 462 463 464 <dd> <a href="https://golang.org/pkg/net/http/#Request.ProtoAtLeast">func (r *Request) ProtoAtLeast(major, minor int) bool</a></dd> 465 466 467 <dd> <a href="https://golang.org/pkg/net/http/#Request.Referer">func (r *Request) Referer() string</a></dd> 468 469 470 <dd> <a href="https://golang.org/pkg/net/http/#Request.SetBasicAuth">func (r *Request) SetBasicAuth(username, password string)</a></dd> 471 472 473 <dd> <a href="https://golang.org/pkg/net/http/#Request.UserAgent">func (r *Request) UserAgent() string</a></dd> 474 475 476 <dd> <a href="https://golang.org/pkg/net/http/#Request.Write">func (r *Request) Write(w io.Writer) error</a></dd> 477 478 479 <dd> <a href="https://golang.org/pkg/net/http/#Request.WriteProxy">func (r *Request) WriteProxy(w io.Writer) error</a></dd> 480 481 482 483 <dd><a href="https://golang.org/pkg/net/http/#Response">type Response</a></dd> 484 485 486 <dd> <a href="https://golang.org/pkg/net/http/#Get">func Get(url string) (resp *Response, err error)</a></dd> 487 488 489 <dd> <a href="https://golang.org/pkg/net/http/#Head">func Head(url string) (resp *Response, err error)</a></dd> 490 491 492 <dd> <a href="https://golang.org/pkg/net/http/#Post">func Post(url string, bodyType string, body io.Reader) (resp *Response, err error)</a></dd> 493 494 495 <dd> <a href="https://golang.org/pkg/net/http/#PostForm">func PostForm(url string, data url.Values) (resp *Response, err error)</a></dd> 496 497 498 <dd> <a href="https://golang.org/pkg/net/http/#ReadResponse">func ReadResponse(r *bufio.Reader, req *Request) (*Response, error)</a></dd> 499 500 501 502 <dd> <a href="https://golang.org/pkg/net/http/#Response.Cookies">func (r *Response) Cookies() []*Cookie</a></dd> 503 504 505 <dd> <a href="https://golang.org/pkg/net/http/#Response.Location">func (r *Response) Location() (*url.URL, error)</a></dd> 506 507 508 <dd> <a href="https://golang.org/pkg/net/http/#Response.ProtoAtLeast">func (r *Response) ProtoAtLeast(major, minor int) bool</a></dd> 509 510 511 <dd> <a href="https://golang.org/pkg/net/http/#Response.Write">func (r *Response) Write(w io.Writer) error</a></dd> 512 513 514 515 <dd><a href="https://golang.org/pkg/net/http/#ResponseWriter">type ResponseWriter</a></dd> 516 517 518 519 520 <dd><a href="https://golang.org/pkg/net/http/#RoundTripper">type RoundTripper</a></dd> 521 522 523 <dd> <a href="https://golang.org/pkg/net/http/#NewFileTransport">func NewFileTransport(fs FileSystem) RoundTripper</a></dd> 524 525 526 527 528 <dd><a href="https://golang.org/pkg/net/http/#ServeMux">type ServeMux</a></dd> 529 530 531 <dd> <a href="https://golang.org/pkg/net/http/#NewServeMux">func NewServeMux() *ServeMux</a></dd> 532 533 534 535 <dd> <a href="https://golang.org/pkg/net/http/#ServeMux.Handle">func (mux *ServeMux) Handle(pattern string, handler Handler)</a></dd> 536 537 538 <dd> <a href="https://golang.org/pkg/net/http/#ServeMux.HandleFunc">func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request))</a></dd> 539 540 541 <dd> <a href="https://golang.org/pkg/net/http/#ServeMux.Handler">func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string)</a></dd> 542 543 544 <dd> <a href="https://golang.org/pkg/net/http/#ServeMux.ServeHTTP">func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request)</a></dd> 545 546 547 548 <dd><a href="https://golang.org/pkg/net/http/#Server">type Server</a></dd> 549 550 551 552 <dd> <a href="https://golang.org/pkg/net/http/#Server.ListenAndServe">func (srv *Server) ListenAndServe() error</a></dd> 553 554 555 <dd> <a href="https://golang.org/pkg/net/http/#Server.ListenAndServeTLS">func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error</a></dd> 556 557 558 <dd> <a href="https://golang.org/pkg/net/http/#Server.Serve">func (srv *Server) Serve(l net.Listener) error</a></dd> 559 560 561 <dd> <a href="https://golang.org/pkg/net/http/#Server.SetKeepAlivesEnabled">func (srv *Server) SetKeepAlivesEnabled(v bool)</a></dd> 562 563 564 565 <dd><a href="https://golang.org/pkg/net/http/#Transport">type Transport</a></dd> 566 567 568 569 <dd> <a href="https://golang.org/pkg/net/http/#Transport.CancelRequest">func (t *Transport) CancelRequest(req *Request)</a></dd> 570 571 572 <dd> <a href="https://golang.org/pkg/net/http/#Transport.CloseIdleConnections">func (t *Transport) CloseIdleConnections()</a></dd> 573 574 575 <dd> <a href="https://golang.org/pkg/net/http/#Transport.RegisterProtocol">func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper)</a></dd> 576 577 578 <dd> <a href="https://golang.org/pkg/net/http/#Transport.RoundTrip">func (t *Transport) RoundTrip(req *Request) (resp *Response, err error)</a></dd> 579 580 581 582 </dl> 583 </div><!-- #manual-nav --> 584 585 586 <div id="pkg-examples"> 587 <h4>Examples</h4> 588 <dl> 589 590 <dd><a class="exampleLink" href="https://golang.org/pkg/net/http/#example_FileServer">FileServer</a></dd> 591 592 <dd><a class="exampleLink" href="https://golang.org/pkg/net/http/#example_FileServer_stripPrefix">FileServer (StripPrefix)</a></dd> 593 594 <dd><a class="exampleLink" href="https://golang.org/pkg/net/http/#example_Get">Get</a></dd> 595 596 <dd><a class="exampleLink" href="https://golang.org/pkg/net/http/#example_Hijacker">Hijacker</a></dd> 597 598 <dd><a class="exampleLink" href="https://golang.org/pkg/net/http/#example_ResponseWriter_trailers">ResponseWriter (Trailers)</a></dd> 599 600 <dd><a class="exampleLink" href="https://golang.org/pkg/net/http/#example_ServeMux_Handle">ServeMux.Handle</a></dd> 601 602 <dd><a class="exampleLink" href="https://golang.org/pkg/net/http/#example_StripPrefix">StripPrefix</a></dd> 603 604 </dl> 605 </div> 606 607 608 609 <h4>Package files</h4> 610 <p> 611 <span style="font-size:90%"> 612 613 <a href="https://golang.org/src/net/http/client.go">client.go</a> 614 615 <a href="https://golang.org/src/net/http/cookie.go">cookie.go</a> 616 617 <a href="https://golang.org/src/net/http/doc.go">doc.go</a> 618 619 <a href="https://golang.org/src/net/http/filetransport.go">filetransport.go</a> 620 621 <a href="https://golang.org/src/net/http/fs.go">fs.go</a> 622 623 <a href="https://golang.org/src/net/http/header.go">header.go</a> 624 625 <a href="https://golang.org/src/net/http/jar.go">jar.go</a> 626 627 <a href="https://golang.org/src/net/http/lex.go">lex.go</a> 628 629 <a href="https://golang.org/src/net/http/request.go">request.go</a> 630 631 <a href="https://golang.org/src/net/http/response.go">response.go</a> 632 633 <a href="https://golang.org/src/net/http/server.go">server.go</a> 634 635 <a href="https://golang.org/src/net/http/sniff.go">sniff.go</a> 636 637 <a href="https://golang.org/src/net/http/status.go">status.go</a> 638 639 <a href="https://golang.org/src/net/http/transfer.go">transfer.go</a> 640 641 <a href="https://golang.org/src/net/http/transport.go">transport.go</a> 642 643 </span> 644 </p> 645 646 </div><!-- .expanded --> 647 </div><!-- #pkg-index --> 648 649 <div id="pkg-callgraph" class="toggle" style="display: none"> 650 <div class="collapsed"> 651 <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2> 652 </div> <!-- .expanded --> 653 <div class="expanded"> 654 <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2> 655 <p> 656 In the call graph viewer below, each node 657 is a function belonging to this package 658 and its children are the functions it 659 calls—perhaps dynamically. 660 </p> 661 <p> 662 The root nodes are the entry points of the 663 package: functions that may be called from 664 outside the package. 665 There may be non-exported or anonymous 666 functions among them if they are called 667 dynamically from another package. 668 </p> 669 <p> 670 Click a node to visit that function's source code. 671 From there you can visit its callers by 672 clicking its declaring <code>func</code> 673 token. 674 </p> 675 <p> 676 Functions may be omitted if they were 677 determined to be unreachable in the 678 particular programs or tests that were 679 analyzed. 680 </p> 681 <!-- Zero means show all package entry points. --> 682 <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul> 683 </div> 684 </div> <!-- #pkg-callgraph --> 685 686 687 <h2 id="pkg-constants">Constants</h2> 688 689 <pre>const ( 690 <span id="StatusContinue">StatusContinue</span> = 100 691 <span id="StatusSwitchingProtocols">StatusSwitchingProtocols</span> = 101 692 693 <span id="StatusOK">StatusOK</span> = 200 694 <span id="StatusCreated">StatusCreated</span> = 201 695 <span id="StatusAccepted">StatusAccepted</span> = 202 696 <span id="StatusNonAuthoritativeInfo">StatusNonAuthoritativeInfo</span> = 203 697 <span id="StatusNoContent">StatusNoContent</span> = 204 698 <span id="StatusResetContent">StatusResetContent</span> = 205 699 <span id="StatusPartialContent">StatusPartialContent</span> = 206 700 701 <span id="StatusMultipleChoices">StatusMultipleChoices</span> = 300 702 <span id="StatusMovedPermanently">StatusMovedPermanently</span> = 301 703 <span id="StatusFound">StatusFound</span> = 302 704 <span id="StatusSeeOther">StatusSeeOther</span> = 303 705 <span id="StatusNotModified">StatusNotModified</span> = 304 706 <span id="StatusUseProxy">StatusUseProxy</span> = 305 707 <span id="StatusTemporaryRedirect">StatusTemporaryRedirect</span> = 307 708 709 <span id="StatusBadRequest">StatusBadRequest</span> = 400 710 <span id="StatusUnauthorized">StatusUnauthorized</span> = 401 711 <span id="StatusPaymentRequired">StatusPaymentRequired</span> = 402 712 <span id="StatusForbidden">StatusForbidden</span> = 403 713 <span id="StatusNotFound">StatusNotFound</span> = 404 714 <span id="StatusMethodNotAllowed">StatusMethodNotAllowed</span> = 405 715 <span id="StatusNotAcceptable">StatusNotAcceptable</span> = 406 716 <span id="StatusProxyAuthRequired">StatusProxyAuthRequired</span> = 407 717 <span id="StatusRequestTimeout">StatusRequestTimeout</span> = 408 718 <span id="StatusConflict">StatusConflict</span> = 409 719 <span id="StatusGone">StatusGone</span> = 410 720 <span id="StatusLengthRequired">StatusLengthRequired</span> = 411 721 <span id="StatusPreconditionFailed">StatusPreconditionFailed</span> = 412 722 <span id="StatusRequestEntityTooLarge">StatusRequestEntityTooLarge</span> = 413 723 <span id="StatusRequestURITooLong">StatusRequestURITooLong</span> = 414 724 <span id="StatusUnsupportedMediaType">StatusUnsupportedMediaType</span> = 415 725 <span id="StatusRequestedRangeNotSatisfiable">StatusRequestedRangeNotSatisfiable</span> = 416 726 <span id="StatusExpectationFailed">StatusExpectationFailed</span> = 417 727 <span id="StatusTeapot">StatusTeapot</span> = 418 728 729 <span id="StatusInternalServerError">StatusInternalServerError</span> = 500 730 <span id="StatusNotImplemented">StatusNotImplemented</span> = 501 731 <span id="StatusBadGateway">StatusBadGateway</span> = 502 732 <span id="StatusServiceUnavailable">StatusServiceUnavailable</span> = 503 733 <span id="StatusGatewayTimeout">StatusGatewayTimeout</span> = 504 734 <span id="StatusHTTPVersionNotSupported">StatusHTTPVersionNotSupported</span> = 505 735 )</pre> 736 <p> 737 HTTP status codes, defined in RFC 2616. 738 </p> 739 740 741 <pre>const <span id="DefaultMaxHeaderBytes">DefaultMaxHeaderBytes</span> = 1 << 20 <span class="comment">// 1 MB</span> 742 </pre> 743 <p> 744 DefaultMaxHeaderBytes is the maximum permitted size of the headers 745 in an HTTP request. 746 This can be overridden by setting Server.MaxHeaderBytes. 747 </p> 748 749 750 <pre>const <span id="DefaultMaxIdleConnsPerHost">DefaultMaxIdleConnsPerHost</span> = 2</pre> 751 <p> 752 DefaultMaxIdleConnsPerHost is the default value of Transport's 753 MaxIdleConnsPerHost. 754 </p> 755 756 757 <pre>const <span id="TimeFormat">TimeFormat</span> = "Mon, 02 Jan 2006 15:04:05 GMT"</pre> 758 <p> 759 TimeFormat is the time format to use with 760 time.Parse and time.Time.Format when parsing 761 or generating times in HTTP headers. 762 It is like time.RFC1123 but hard codes GMT as the time zone. 763 </p> 764 765 766 767 768 <h2 id="pkg-variables">Variables</h2> 769 770 <pre>var ( 771 <span id="ErrHeaderTooLong">ErrHeaderTooLong</span> = &<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>{"header too long"} 772 <span id="ErrShortBody">ErrShortBody</span> = &<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>{"entity body too short"} 773 <span id="ErrNotSupported">ErrNotSupported</span> = &<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>{"feature not supported"} 774 <span id="ErrUnexpectedTrailer">ErrUnexpectedTrailer</span> = &<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>{"trailer header without chunked transfer encoding"} 775 <span id="ErrMissingContentLength">ErrMissingContentLength</span> = &<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>{"missing ContentLength in HEAD response"} 776 <span id="ErrNotMultipart">ErrNotMultipart</span> = &<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>{"request Content-Type isn't multipart/form-data"} 777 <span id="ErrMissingBoundary">ErrMissingBoundary</span> = &<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>{"no multipart boundary param in Content-Type"} 778 )</pre> 779 780 781 <pre>var ( 782 <span id="ErrWriteAfterFlush">ErrWriteAfterFlush</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("Conn.Write called after Flush") 783 <span id="ErrBodyNotAllowed">ErrBodyNotAllowed</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("http: request method or response status code does not allow body") 784 <span id="ErrHijacked">ErrHijacked</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("Conn has been hijacked") 785 <span id="ErrContentLength">ErrContentLength</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("Conn.Write wrote more than the declared Content-Length") 786 )</pre> 787 <p> 788 Errors introduced by the HTTP server. 789 </p> 790 791 792 <pre>var <span id="DefaultClient">DefaultClient</span> = &<a href="https://golang.org/pkg/net/http/#Client">Client</a>{}</pre> 793 <p> 794 DefaultClient is the default Client and is used by Get, Head, and Post. 795 </p> 796 797 798 <pre>var <span id="DefaultServeMux">DefaultServeMux</span> = <a href="https://golang.org/pkg/net/http/#NewServeMux">NewServeMux</a>()</pre> 799 <p> 800 DefaultServeMux is the default ServeMux used by Serve. 801 </p> 802 803 804 <pre>var <span id="ErrBodyReadAfterClose">ErrBodyReadAfterClose</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("http: invalid Read on closed Body")</pre> 805 <p> 806 ErrBodyReadAfterClose is returned when reading a Request or Response 807 Body after the body has been closed. This typically happens when the body is 808 read after an HTTP Handler calls WriteHeader or Write on its 809 ResponseWriter. 810 </p> 811 812 813 <pre>var <span id="ErrHandlerTimeout">ErrHandlerTimeout</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("http: Handler timeout")</pre> 814 <p> 815 ErrHandlerTimeout is returned on ResponseWriter Write calls 816 in handlers which have timed out. 817 </p> 818 819 820 <pre>var <span id="ErrLineTooLong">ErrLineTooLong</span> = <a href="https://golang.org/pkg/net/http/internal/">internal</a>.<a href="https://golang.org/pkg/net/http/internal/#ErrLineTooLong">ErrLineTooLong</a></pre> 821 <p> 822 ErrLineTooLong is returned when reading request or response bodies 823 with malformed chunked encoding. 824 </p> 825 826 827 <pre>var <span id="ErrMissingFile">ErrMissingFile</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("http: no such file")</pre> 828 <p> 829 ErrMissingFile is returned by FormFile when the provided file field name 830 is either not present in the request or not a file field. 831 </p> 832 833 834 <pre>var <span id="ErrNoCookie">ErrNoCookie</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("http: named cookie not present")</pre> 835 <p> 836 ErrNoCookie is returned by Request's Cookie method when a cookie is not found. 837 </p> 838 839 840 <pre>var <span id="ErrNoLocation">ErrNoLocation</span> = <a href="https://golang.org/pkg/errors/">errors</a>.<a href="https://golang.org/pkg/errors/#New">New</a>("http: no Location header in response")</pre> 841 <p> 842 ErrNoLocation is returned by Response's Location method 843 when no Location header is present. 844 </p> 845 846 847 848 849 850 851 <h2 id="CanonicalHeaderKey">func <a href="https://golang.org/src/net/http/header.go?s=4562:4602#L163">CanonicalHeaderKey</a></h2> 852 <pre>func CanonicalHeaderKey(s <a href="https://golang.org/pkg/builtin/#string">string</a>) <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 853 <p> 854 CanonicalHeaderKey returns the canonical format of the 855 header key s. The canonicalization converts the first 856 letter and any letter following a hyphen to upper case; 857 the rest are converted to lowercase. For example, the 858 canonical key for "accept-encoding" is "Accept-Encoding". 859 If s contains a space or invalid header field bytes, it is 860 returned without modifications. 861 </p> 862 863 864 865 866 867 868 869 <h2 id="DetectContentType">func <a href="https://golang.org/src/net/http/sniff.go?s=648:690#L11">DetectContentType</a></h2> 870 <pre>func DetectContentType(data []<a href="https://golang.org/pkg/builtin/#byte">byte</a>) <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 871 <p> 872 DetectContentType implements the algorithm described 873 at <a href="http://mimesniff.spec.whatwg.org/">http://mimesniff.spec.whatwg.org/</a> to determine the 874 Content-Type of the given data. It considers at most the 875 first 512 bytes of data. DetectContentType always returns 876 a valid MIME type: if it cannot determine a more specific one, it 877 returns "application/octet-stream". 878 </p> 879 880 881 882 883 884 885 886 <h2 id="Error">func <a href="https://golang.org/src/net/http/server.go?s=41562:41614#L1419">Error</a></h2> 887 <pre>func Error(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, error <a href="https://golang.org/pkg/builtin/#string">string</a>, code <a href="https://golang.org/pkg/builtin/#int">int</a>)</pre> 888 <p> 889 Error replies to the request with the specified error message and HTTP code. 890 The error message should be plain text. 891 </p> 892 893 894 895 896 897 898 899 <h2 id="Handle">func <a href="https://golang.org/src/net/http/server.go?s=51434:51478#L1740">Handle</a></h2> 900 <pre>func Handle(pattern <a href="https://golang.org/pkg/builtin/#string">string</a>, handler <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>)</pre> 901 <p> 902 Handle registers the handler for the given pattern 903 in the DefaultServeMux. 904 The documentation for ServeMux explains how patterns are matched. 905 </p> 906 907 908 909 910 911 912 913 <h2 id="HandleFunc">func <a href="https://golang.org/src/net/http/server.go?s=51688:51759#L1745">HandleFunc</a></h2> 914 <pre>func HandleFunc(pattern <a href="https://golang.org/pkg/builtin/#string">string</a>, handler func(<a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, *<a href="https://golang.org/pkg/net/http/#Request">Request</a>))</pre> 915 <p> 916 HandleFunc registers the handler function for the given pattern 917 in the DefaultServeMux. 918 The documentation for ServeMux explains how patterns are matched. 919 </p> 920 921 922 923 924 925 926 927 <h2 id="ListenAndServe">func <a href="https://golang.org/src/net/http/server.go?s=58314:58369#L1955">ListenAndServe</a></h2> 928 <pre>func ListenAndServe(addr <a href="https://golang.org/pkg/builtin/#string">string</a>, handler <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 929 <p> 930 ListenAndServe listens on the TCP network address addr 931 and then calls Serve with handler to handle requests 932 on incoming connections. Handler is typically nil, 933 in which case the DefaultServeMux is used. 934 </p> 935 <p> 936 A trivial example server is: 937 </p> 938 <pre>package main 939 940 import ( 941 "io" 942 "net/http" 943 "log" 944 ) 945 946 // hello world, the web server 947 func HelloServer(w http.ResponseWriter, req *http.Request) { 948 io.WriteString(w, "hello, world!\n") 949 } 950 951 func main() { 952 http.HandleFunc("/hello", HelloServer) 953 err := http.ListenAndServe(":12345", nil) 954 if err != nil { 955 log.Fatal("ListenAndServe: ", err) 956 } 957 } 958 </pre> 959 960 961 962 963 964 965 966 <h2 id="ListenAndServeTLS">func <a href="https://golang.org/src/net/http/server.go?s=59426:59517#L1988">ListenAndServeTLS</a></h2> 967 <pre>func ListenAndServeTLS(addr <a href="https://golang.org/pkg/builtin/#string">string</a>, certFile <a href="https://golang.org/pkg/builtin/#string">string</a>, keyFile <a href="https://golang.org/pkg/builtin/#string">string</a>, handler <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 968 <p> 969 ListenAndServeTLS acts identically to ListenAndServe, except that it 970 expects HTTPS connections. Additionally, files containing a certificate and 971 matching private key for the server must be provided. If the certificate 972 is signed by a certificate authority, the certFile should be the concatenation 973 of the server's certificate, any intermediates, and the CA's certificate. 974 </p> 975 <p> 976 A trivial example server is: 977 </p> 978 <pre>import ( 979 "log" 980 "net/http" 981 ) 982 983 func handler(w http.ResponseWriter, req *http.Request) { 984 w.Header().Set("Content-Type", "text/plain") 985 w.Write([]byte("This is an example server.\n")) 986 } 987 988 func main() { 989 http.HandleFunc("/", handler) 990 log.Printf("About to listen on 10443. Go to <a href="https://127.0.0.1:10443/">https://127.0.0.1:10443/</a>") 991 err := http.ListenAndServeTLS(":10443", "cert.pem", "key.pem", nil) 992 if err != nil { 993 log.Fatal(err) 994 } 995 } 996 </pre> 997 <p> 998 One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem. 999 </p> 1000 1001 1002 1003 1004 1005 1006 1007 <h2 id="MaxBytesReader">func <a href="https://golang.org/src/net/http/request.go?s=22939:23016#L726">MaxBytesReader</a></h2> 1008 <pre>func MaxBytesReader(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, r <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#ReadCloser">ReadCloser</a>, n <a href="https://golang.org/pkg/builtin/#int64">int64</a>) <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#ReadCloser">ReadCloser</a></pre> 1009 <p> 1010 MaxBytesReader is similar to io.LimitReader but is intended for 1011 limiting the size of incoming request bodies. In contrast to 1012 io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a 1013 non-EOF error for a Read beyond the limit, and closes the 1014 underlying reader when its Close method is called. 1015 </p> 1016 <p> 1017 MaxBytesReader prevents clients from accidentally or maliciously 1018 sending a large request and wasting server resources. 1019 </p> 1020 1021 1022 1023 1024 1025 1026 1027 <h2 id="NotFound">func <a href="https://golang.org/src/net/http/server.go?s=41848:41891#L1427">NotFound</a></h2> 1028 <pre>func NotFound(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>)</pre> 1029 <p> 1030 NotFound replies to the request with an HTTP 404 not found error. 1031 </p> 1032 1033 1034 1035 1036 1037 1038 1039 <h2 id="ParseHTTPVersion">func <a href="https://golang.org/src/net/http/request.go?s=16539:16601#L498">ParseHTTPVersion</a></h2> 1040 <pre>func ParseHTTPVersion(vers <a href="https://golang.org/pkg/builtin/#string">string</a>) (major, minor <a href="https://golang.org/pkg/builtin/#int">int</a>, ok <a href="https://golang.org/pkg/builtin/#bool">bool</a>)</pre> 1041 <p> 1042 ParseHTTPVersion parses a HTTP version string. 1043 "HTTP/1.0" returns (1, 0, true). 1044 </p> 1045 1046 1047 1048 1049 1050 1051 1052 <h2 id="ParseTime">func <a href="https://golang.org/src/net/http/header.go?s=1908:1960#L69">ParseTime</a></h2> 1053 <pre>func ParseTime(text <a href="https://golang.org/pkg/builtin/#string">string</a>) (t <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Time">Time</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1054 <p> 1055 ParseTime parses a time header (such as the Date: header), 1056 trying each of the three formats allowed by HTTP/1.1: 1057 TimeFormat, time.RFC850, and time.ANSIC. 1058 </p> 1059 1060 1061 1062 1063 1064 1065 1066 <h2 id="ProxyFromEnvironment">func <a href="https://golang.org/src/net/http/transport.go?s=4805:4862#L126">ProxyFromEnvironment</a></h2> 1067 <pre>func ProxyFromEnvironment(req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (*<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1068 <p> 1069 ProxyFromEnvironment returns the URL of the proxy to use for a 1070 given request, as indicated by the environment variables 1071 HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions 1072 thereof). HTTPS_PROXY takes precedence over HTTP_PROXY for https 1073 requests. 1074 </p> 1075 <p> 1076 The environment values may be either a complete URL or a 1077 "host[:port]", in which case the "http" scheme is assumed. 1078 An error is returned if the value is a different form. 1079 </p> 1080 <p> 1081 A nil URL and nil error are returned if no proxy is defined in the 1082 environment, or a proxy should not be used for the given request, 1083 as defined by NO_PROXY. 1084 </p> 1085 <p> 1086 As a special case, if req.URL.Host is "localhost" (with or without 1087 a port number), then a nil URL and nil error will be returned. 1088 </p> 1089 1090 1091 1092 1093 1094 1095 1096 <h2 id="ProxyURL">func <a href="https://golang.org/src/net/http/transport.go?s=5664:5729#L157">ProxyURL</a></h2> 1097 <pre>func ProxyURL(fixedURL *<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a>) func(*<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (*<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1098 <p> 1099 ProxyURL returns a proxy function (for use in a Transport) 1100 that always returns the same URL. 1101 </p> 1102 1103 1104 1105 1106 1107 1108 1109 <h2 id="Redirect">func <a href="https://golang.org/src/net/http/server.go?s=42815:42883#L1454">Redirect</a></h2> 1110 <pre>func Redirect(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>, urlStr <a href="https://golang.org/pkg/builtin/#string">string</a>, code <a href="https://golang.org/pkg/builtin/#int">int</a>)</pre> 1111 <p> 1112 Redirect replies to the request with a redirect to url, 1113 which may be a path relative to the request path. 1114 </p> 1115 1116 1117 1118 1119 1120 1121 1122 <h2 id="Serve">func <a href="https://golang.org/src/net/http/server.go?s=52072:52121#L1753">Serve</a></h2> 1123 <pre>func Serve(l <a href="https://golang.org/pkg/net/">net</a>.<a href="https://golang.org/pkg/net/#Listener">Listener</a>, handler <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 1124 <p> 1125 Serve accepts incoming HTTP connections on the listener l, 1126 creating a new service goroutine for each. The service goroutines 1127 read requests and then call handler to reply to them. 1128 Handler is typically nil, in which case the DefaultServeMux is used. 1129 </p> 1130 1131 1132 1133 1134 1135 1136 1137 <h2 id="ServeContent">func <a href="https://golang.org/src/net/http/fs.go?s=3535:3639#L107">ServeContent</a></h2> 1138 <pre>func ServeContent(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>, name <a href="https://golang.org/pkg/builtin/#string">string</a>, modtime <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Time">Time</a>, content <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#ReadSeeker">ReadSeeker</a>)</pre> 1139 <p> 1140 ServeContent replies to the request using the content in the 1141 provided ReadSeeker. The main benefit of ServeContent over io.Copy 1142 is that it handles Range requests properly, sets the MIME type, and 1143 handles If-Modified-Since requests. 1144 </p> 1145 <p> 1146 If the response's Content-Type header is not set, ServeContent 1147 first tries to deduce the type from name's file extension and, 1148 if that fails, falls back to reading the first block of the content 1149 and passing it to DetectContentType. 1150 The name is otherwise unused; in particular it can be empty and is 1151 never sent in the response. 1152 </p> 1153 <p> 1154 If modtime is not the zero time or Unix epoch, ServeContent 1155 includes it in a Last-Modified header in the response. If the 1156 request includes an If-Modified-Since header, ServeContent uses 1157 modtime to decide whether the content needs to be sent at all. 1158 </p> 1159 <p> 1160 The content's Seek method must work: ServeContent uses 1161 a seek to the end of the content to determine its size. 1162 </p> 1163 <p> 1164 If the caller has set w's ETag header, ServeContent uses it to 1165 handle requests using If-Range and If-None-Match. 1166 </p> 1167 <p> 1168 Note that *os.File implements the io.ReadSeeker interface. 1169 </p> 1170 1171 1172 1173 1174 1175 1176 1177 <h2 id="ServeFile">func <a href="https://golang.org/src/net/http/fs.go?s=13941:13998#L443">ServeFile</a></h2> 1178 <pre>func ServeFile(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>, name <a href="https://golang.org/pkg/builtin/#string">string</a>)</pre> 1179 <p> 1180 ServeFile replies to the request with the contents of the named 1181 file or directory. 1182 </p> 1183 <p> 1184 As a special case, ServeFile redirects any request where r.URL.Path 1185 ends in "/index.html" to the same path, without the final 1186 "index.html". To avoid such redirects either modify the path or 1187 use ServeContent. 1188 </p> 1189 1190 1191 1192 1193 1194 1195 1196 <h2 id="SetCookie">func <a href="https://golang.org/src/net/http/cookie.go?s=3059:3107#L120">SetCookie</a></h2> 1197 <pre>func SetCookie(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, cookie *<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a>)</pre> 1198 <p> 1199 SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers. 1200 The provided cookie must have a valid Name. Invalid cookies may be 1201 silently dropped. 1202 </p> 1203 1204 1205 1206 1207 1208 1209 1210 <h2 id="StatusText">func <a href="https://golang.org/src/net/http/status.go?s=4602:4634#L108">StatusText</a></h2> 1211 <pre>func StatusText(code <a href="https://golang.org/pkg/builtin/#int">int</a>) <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 1212 <p> 1213 StatusText returns a text for the HTTP status code. It returns the empty 1214 string if the code is unknown. 1215 </p> 1216 1217 1218 1219 1220 1221 1222 1223 1224 <h2 id="Client">type <a href="https://golang.org/src/net/http/client.go?s=897:2420#L26">Client</a></h2> 1225 <pre>type Client struct { 1226 <span class="comment">// Transport specifies the mechanism by which individual</span> 1227 <span class="comment">// HTTP requests are made.</span> 1228 <span class="comment">// If nil, DefaultTransport is used.</span> 1229 Transport <a href="https://golang.org/pkg/net/http/#RoundTripper">RoundTripper</a> 1230 1231 <span class="comment">// CheckRedirect specifies the policy for handling redirects.</span> 1232 <span class="comment">// If CheckRedirect is not nil, the client calls it before</span> 1233 <span class="comment">// following an HTTP redirect. The arguments req and via are</span> 1234 <span class="comment">// the upcoming request and the requests made already, oldest</span> 1235 <span class="comment">// first. If CheckRedirect returns an error, the Client's Get</span> 1236 <span class="comment">// method returns both the previous Response and</span> 1237 <span class="comment">// CheckRedirect's error (wrapped in a url.Error) instead of</span> 1238 <span class="comment">// issuing the Request req.</span> 1239 <span class="comment">//</span> 1240 <span class="comment">// If CheckRedirect is nil, the Client uses its default policy,</span> 1241 <span class="comment">// which is to stop after 10 consecutive requests.</span> 1242 CheckRedirect func(req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>, via []*<a href="https://golang.org/pkg/net/http/#Request">Request</a>) <a href="https://golang.org/pkg/builtin/#error">error</a> 1243 1244 <span class="comment">// Jar specifies the cookie jar.</span> 1245 <span class="comment">// If Jar is nil, cookies are not sent in requests and ignored</span> 1246 <span class="comment">// in responses.</span> 1247 Jar <a href="https://golang.org/pkg/net/http/#CookieJar">CookieJar</a> 1248 1249 <span class="comment">// Timeout specifies a time limit for requests made by this</span> 1250 <span class="comment">// Client. The timeout includes connection time, any</span> 1251 <span class="comment">// redirects, and reading the response body. The timer remains</span> 1252 <span class="comment">// running after Get, Head, Post, or Do return and will</span> 1253 <span class="comment">// interrupt reading of the Response.Body.</span> 1254 <span class="comment">//</span> 1255 <span class="comment">// A Timeout of zero means no timeout.</span> 1256 <span class="comment">//</span> 1257 <span class="comment">// The Client's Transport must support the CancelRequest</span> 1258 <span class="comment">// method or Client will return errors when attempting to make</span> 1259 <span class="comment">// a request with Get, Head, Post, or Do. Client's default</span> 1260 <span class="comment">// Transport (DefaultTransport) supports CancelRequest.</span> 1261 Timeout <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Duration">Duration</a> 1262 }</pre> 1263 <p> 1264 A Client is an HTTP client. Its zero value (DefaultClient) is a 1265 usable client that uses DefaultTransport. 1266 </p> 1267 <p> 1268 The Client's Transport typically has internal state (cached TCP 1269 connections), so Clients should be reused instead of created as 1270 needed. Clients are safe for concurrent use by multiple goroutines. 1271 </p> 1272 <p> 1273 A Client is higher-level than a RoundTripper (such as Transport) 1274 and additionally handles HTTP details such as cookies and 1275 redirects. 1276 </p> 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 <h3 id="Client.Do">func (*Client) <a href="https://golang.org/src/net/http/client.go?s=6009:6070#L163">Do</a></h3> 1292 <pre>func (c *<a href="https://golang.org/pkg/net/http/#Client">Client</a>) Do(req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1293 <p> 1294 Do sends an HTTP request and returns an HTTP response, following 1295 policy (e.g. redirects, cookies, auth) as configured on the client. 1296 </p> 1297 <p> 1298 An error is returned if caused by client policy (such as 1299 CheckRedirect), or if there was an HTTP protocol error. 1300 A non-2xx response doesn't cause an error. 1301 </p> 1302 <p> 1303 When err is nil, resp always contains a non-nil resp.Body. 1304 </p> 1305 <p> 1306 Callers should close resp.Body when done reading from it. If 1307 resp.Body is not closed, the Client's underlying RoundTripper 1308 (typically Transport) may not be able to re-use a persistent TCP 1309 connection to the server for a subsequent "keep-alive" request. 1310 </p> 1311 <p> 1312 The request Body, if non-nil, will be closed by the underlying 1313 Transport, even on errors. 1314 </p> 1315 <p> 1316 Generally Get, Post, or PostForm will be used instead of Do. 1317 </p> 1318 1319 1320 1321 1322 1323 1324 <h3 id="Client.Get">func (*Client) <a href="https://golang.org/src/net/http/client.go?s=9928:9988#L291">Get</a></h3> 1325 <pre>func (c *<a href="https://golang.org/pkg/net/http/#Client">Client</a>) Get(url <a href="https://golang.org/pkg/builtin/#string">string</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1326 <p> 1327 Get issues a GET to the specified URL. If the response is one of the 1328 following redirect codes, Get follows the redirect after calling the 1329 Client's CheckRedirect function: 1330 </p> 1331 <pre>301 (Moved Permanently) 1332 302 (Found) 1333 303 (See Other) 1334 307 (Temporary Redirect) 1335 </pre> 1336 <p> 1337 An error is returned if the Client's CheckRedirect function fails 1338 or if there was an HTTP protocol error. A non-2xx response doesn't 1339 cause an error. 1340 </p> 1341 <p> 1342 When err is nil, resp always contains a non-nil resp.Body. 1343 Caller should close resp.Body when done reading from it. 1344 </p> 1345 <p> 1346 To make a request with custom headers, use NewRequest and Client.Do. 1347 </p> 1348 1349 1350 1351 1352 1353 1354 <h3 id="Client.Head">func (*Client) <a href="https://golang.org/src/net/http/client.go?s=16276:16337#L512">Head</a></h3> 1355 <pre>func (c *<a href="https://golang.org/pkg/net/http/#Client">Client</a>) Head(url <a href="https://golang.org/pkg/builtin/#string">string</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1356 <p> 1357 Head issues a HEAD to the specified URL. If the response is one of the 1358 following redirect codes, Head follows the redirect after calling the 1359 Client's CheckRedirect function: 1360 </p> 1361 <pre>301 (Moved Permanently) 1362 302 (Found) 1363 303 (See Other) 1364 307 (Temporary Redirect) 1365 </pre> 1366 1367 1368 1369 1370 1371 1372 <h3 id="Client.Post">func (*Client) <a href="https://golang.org/src/net/http/client.go?s=14197:14291#L455">Post</a></h3> 1373 <pre>func (c *<a href="https://golang.org/pkg/net/http/#Client">Client</a>) Post(url <a href="https://golang.org/pkg/builtin/#string">string</a>, bodyType <a href="https://golang.org/pkg/builtin/#string">string</a>, body <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Reader">Reader</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1374 <p> 1375 Post issues a POST to the specified URL. 1376 </p> 1377 <p> 1378 Caller should close resp.Body when done reading from it. 1379 </p> 1380 <p> 1381 If the provided body is an io.Closer, it is closed after the 1382 request. 1383 </p> 1384 <p> 1385 To set custom headers, use NewRequest and Client.Do. 1386 </p> 1387 1388 1389 1390 1391 1392 1393 <h3 id="Client.PostForm">func (*Client) <a href="https://golang.org/src/net/http/client.go?s=15401:15483#L486">PostForm</a></h3> 1394 <pre>func (c *<a href="https://golang.org/pkg/net/http/#Client">Client</a>) PostForm(url <a href="https://golang.org/pkg/builtin/#string">string</a>, data <a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#Values">Values</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1395 <p> 1396 PostForm issues a POST to the specified URL, 1397 with data's keys and values URL-encoded as the request body. 1398 </p> 1399 <p> 1400 The Content-Type header is set to application/x-www-form-urlencoded. 1401 To set other headers, use NewRequest and DefaultClient.Do. 1402 </p> 1403 <p> 1404 When err is nil, resp always contains a non-nil resp.Body. 1405 Caller should close resp.Body when done reading from it. 1406 </p> 1407 1408 1409 1410 1411 1412 1413 1414 1415 <h2 id="CloseNotifier">type <a href="https://golang.org/src/net/http/server.go?s=3931:4099#L106">CloseNotifier</a></h2> 1416 <pre>type CloseNotifier interface { 1417 <span class="comment">// CloseNotify returns a channel that receives a single value</span> 1418 <span class="comment">// when the client connection has gone away.</span> 1419 CloseNotify() <-chan <a href="https://golang.org/pkg/builtin/#bool">bool</a> 1420 }</pre> 1421 <p> 1422 The CloseNotifier interface is implemented by ResponseWriters which 1423 allow detecting when the underlying connection has gone away. 1424 </p> 1425 <p> 1426 This mechanism can be used to cancel long operations on the server 1427 if the client has disconnected before the response is ready. 1428 </p> 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 <h2 id="ConnState">type <a href="https://golang.org/src/net/http/server.go?s=53915:53933#L1793">ConnState</a></h2> 1446 <pre>type ConnState <a href="https://golang.org/pkg/builtin/#int">int</a></pre> 1447 <p> 1448 A ConnState represents the state of a client connection to a server. 1449 It's used by the optional Server.ConnState hook. 1450 </p> 1451 1452 1453 1454 <pre>const ( 1455 <span class="comment">// StateNew represents a new connection that is expected to</span> 1456 <span class="comment">// send a request immediately. Connections begin at this</span> 1457 <span class="comment">// state and then transition to either StateActive or</span> 1458 <span class="comment">// StateClosed.</span> 1459 <span id="StateNew">StateNew</span> <a href="https://golang.org/pkg/net/http/#ConnState">ConnState</a> = <a href="https://golang.org/pkg/builtin/#iota">iota</a> 1460 1461 <span class="comment">// StateActive represents a connection that has read 1 or more</span> 1462 <span class="comment">// bytes of a request. The Server.ConnState hook for</span> 1463 <span class="comment">// StateActive fires before the request has entered a handler</span> 1464 <span class="comment">// and doesn't fire again until the request has been</span> 1465 <span class="comment">// handled. After the request is handled, the state</span> 1466 <span class="comment">// transitions to StateClosed, StateHijacked, or StateIdle.</span> 1467 <span id="StateActive">StateActive</span> 1468 1469 <span class="comment">// StateIdle represents a connection that has finished</span> 1470 <span class="comment">// handling a request and is in the keep-alive state, waiting</span> 1471 <span class="comment">// for a new request. Connections transition from StateIdle</span> 1472 <span class="comment">// to either StateActive or StateClosed.</span> 1473 <span id="StateIdle">StateIdle</span> 1474 1475 <span class="comment">// StateHijacked represents a hijacked connection.</span> 1476 <span class="comment">// This is a terminal state. It does not transition to StateClosed.</span> 1477 <span id="StateHijacked">StateHijacked</span> 1478 1479 <span class="comment">// StateClosed represents a closed connection.</span> 1480 <span class="comment">// This is a terminal state. Hijacked connections do not</span> 1481 <span class="comment">// transition to StateClosed.</span> 1482 <span id="StateClosed">StateClosed</span> 1483 )</pre> 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 <h3 id="ConnState.String">func (ConnState) <a href="https://golang.org/src/net/http/server.go?s=55217:55251#L1834">String</a></h3> 1498 <pre>func (c <a href="https://golang.org/pkg/net/http/#ConnState">ConnState</a>) String() <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 1499 1500 1501 1502 1503 1504 1505 1506 1507 <h2 id="Cookie">type <a href="https://golang.org/src/net/http/cookie.go?s=439:952#L11">Cookie</a></h2> 1508 <pre>type Cookie struct { 1509 Name <a href="https://golang.org/pkg/builtin/#string">string</a> 1510 Value <a href="https://golang.org/pkg/builtin/#string">string</a> 1511 1512 Path <a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// optional</span> 1513 Domain <a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// optional</span> 1514 Expires <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Time">Time</a> <span class="comment">// optional</span> 1515 RawExpires <a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// for reading cookies only</span> 1516 1517 <span class="comment">// MaxAge=0 means no 'Max-Age' attribute specified.</span> 1518 <span class="comment">// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'</span> 1519 <span class="comment">// MaxAge>0 means Max-Age attribute present and given in seconds</span> 1520 MaxAge <a href="https://golang.org/pkg/builtin/#int">int</a> 1521 Secure <a href="https://golang.org/pkg/builtin/#bool">bool</a> 1522 HttpOnly <a href="https://golang.org/pkg/builtin/#bool">bool</a> 1523 Raw <a href="https://golang.org/pkg/builtin/#string">string</a> 1524 Unparsed []<a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// Raw text of unparsed attribute-value pairs</span> 1525 }</pre> 1526 <p> 1527 A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an 1528 HTTP response or the Cookie header of an HTTP request. 1529 </p> 1530 <p> 1531 See <a href="http://tools.ietf.org/html/rfc6265">http://tools.ietf.org/html/rfc6265</a> for details. 1532 </p> 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 <h3 id="Cookie.String">func (*Cookie) <a href="https://golang.org/src/net/http/cookie.go?s=3428:3460#L130">String</a></h3> 1548 <pre>func (c *<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a>) String() <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 1549 <p> 1550 String returns the serialization of the cookie for use in a Cookie 1551 header (if only Name and Value are set) or a Set-Cookie response 1552 header (if other fields are set). 1553 If c is nil or c.Name is invalid, the empty string is returned. 1554 </p> 1555 1556 1557 1558 1559 1560 1561 1562 1563 <h2 id="CookieJar">type <a href="https://golang.org/src/net/http/jar.go?s=433:899#L7">CookieJar</a></h2> 1564 <pre>type CookieJar interface { 1565 <span class="comment">// SetCookies handles the receipt of the cookies in a reply for the</span> 1566 <span class="comment">// given URL. It may or may not choose to save the cookies, depending</span> 1567 <span class="comment">// on the jar's policy and implementation.</span> 1568 SetCookies(u *<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a>, cookies []*<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a>) 1569 1570 <span class="comment">// Cookies returns the cookies to send in a request for the given URL.</span> 1571 <span class="comment">// It is up to the implementation to honor the standard cookie use</span> 1572 <span class="comment">// restrictions such as in RFC 6265.</span> 1573 Cookies(u *<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a>) []*<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a> 1574 }</pre> 1575 <p> 1576 A CookieJar manages storage and use of cookies in HTTP requests. 1577 </p> 1578 <p> 1579 Implementations of CookieJar must be safe for concurrent use by multiple 1580 goroutines. 1581 </p> 1582 <p> 1583 The net/http/cookiejar package provides a CookieJar implementation. 1584 </p> 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 <h2 id="Dir">type <a href="https://golang.org/src/net/http/fs.go?s=719:734#L23">Dir</a></h2> 1602 <pre>type Dir <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 1603 <p> 1604 A Dir implements FileSystem using the native file system restricted to a 1605 specific directory tree. 1606 </p> 1607 <p> 1608 While the FileSystem.Open method takes '/'-separated paths, a Dir's string 1609 value is a filename on the native file system, not a URL, so it is separated 1610 by filepath.Separator, which isn't necessarily '/'. 1611 </p> 1612 <p> 1613 An empty Dir is treated as ".". 1614 </p> 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 <h3 id="Dir.Open">func (Dir) <a href="https://golang.org/src/net/http/fs.go?s=736:780#L25">Open</a></h3> 1630 <pre>func (d <a href="https://golang.org/pkg/net/http/#Dir">Dir</a>) Open(name <a href="https://golang.org/pkg/builtin/#string">string</a>) (<a href="https://golang.org/pkg/net/http/#File">File</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 1631 1632 1633 1634 1635 1636 1637 1638 1639 <h2 id="File">type <a href="https://golang.org/src/net/http/fs.go?s=1591:1755#L52">File</a></h2> 1640 <pre>type File interface { 1641 <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Closer">Closer</a> 1642 <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Reader">Reader</a> 1643 Readdir(count <a href="https://golang.org/pkg/builtin/#int">int</a>) ([]<a href="https://golang.org/pkg/os/">os</a>.<a href="https://golang.org/pkg/os/#FileInfo">FileInfo</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 1644 Seek(offset <a href="https://golang.org/pkg/builtin/#int64">int64</a>, whence <a href="https://golang.org/pkg/builtin/#int">int</a>) (<a href="https://golang.org/pkg/builtin/#int64">int64</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 1645 Stat() (<a href="https://golang.org/pkg/os/">os</a>.<a href="https://golang.org/pkg/os/#FileInfo">FileInfo</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 1646 }</pre> 1647 <p> 1648 A File is returned by a FileSystem's Open method and can be 1649 served by the FileServer implementation. 1650 </p> 1651 <p> 1652 The methods should behave the same as those on an *os.File. 1653 </p> 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 <h2 id="FileSystem">type <a href="https://golang.org/src/net/http/fs.go?s=1354:1416#L44">FileSystem</a></h2> 1671 <pre>type FileSystem interface { 1672 Open(name <a href="https://golang.org/pkg/builtin/#string">string</a>) (<a href="https://golang.org/pkg/net/http/#File">File</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 1673 }</pre> 1674 <p> 1675 A FileSystem implements access to a collection of named files. 1676 The elements in a file path are separated by slash ('/', U+002F) 1677 characters, regardless of host operating system convention. 1678 </p> 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 <h2 id="Flusher">type <a href="https://golang.org/src/net/http/server.go?s=2921:3005#L79">Flusher</a></h2> 1696 <pre>type Flusher interface { 1697 <span class="comment">// Flush sends any buffered data to the client.</span> 1698 Flush() 1699 }</pre> 1700 <p> 1701 The Flusher interface is implemented by ResponseWriters that allow 1702 an HTTP handler to flush buffered data to the client. 1703 </p> 1704 <p> 1705 Note that even for ResponseWriters that support Flush, 1706 if the client is connected through an HTTP proxy, 1707 the buffered data may not reach the client until the response 1708 completes. 1709 </p> 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 <h2 id="Handler">type <a href="https://golang.org/src/net/http/server.go?s=1361:1424#L42">Handler</a></h2> 1727 <pre>type Handler interface { 1728 ServeHTTP(<a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) 1729 }</pre> 1730 <p> 1731 Objects implementing the Handler interface can be 1732 registered to serve a particular path or subtree 1733 in the HTTP server. 1734 </p> 1735 <p> 1736 ServeHTTP should write reply headers and data to the ResponseWriter 1737 and then return. Returning signals that the request is finished 1738 and that the HTTP server can move on to the next request on 1739 the connection. 1740 </p> 1741 <p> 1742 If ServeHTTP panics, the server (the caller of ServeHTTP) assumes 1743 that the effect of the panic was isolated to the active request. 1744 It recovers the panic, logs a stack trace to the server error log, 1745 and hangs up the connection. 1746 </p> 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 <h3 id="FileServer">func <a href="https://golang.org/src/net/http/fs.go?s=14534:14574#L463">FileServer</a></h3> 1760 <pre>func FileServer(root <a href="https://golang.org/pkg/net/http/#FileSystem">FileSystem</a>) <a href="https://golang.org/pkg/net/http/#Handler">Handler</a></pre> 1761 <p> 1762 FileServer returns a handler that serves HTTP requests 1763 with the contents of the file system rooted at root. 1764 </p> 1765 <p> 1766 To use the operating system's file system implementation, 1767 use http.Dir: 1768 </p> 1769 <pre>http.Handle("/", http.FileServer(http.Dir("/tmp"))) 1770 </pre> 1771 <p> 1772 As a special case, the returned file server redirects any request 1773 ending in "/index.html" to the same path, without the final 1774 "index.html". 1775 </p> 1776 1777 <div id="example_FileServer" class="toggle"> 1778 <div class="collapsed"> 1779 <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> 1780 </div> 1781 <div class="expanded"> 1782 <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> 1783 1784 1785 1786 <div class="play"> 1787 <div class="input"><textarea class="code">package main 1788 1789 import ( 1790 "log" 1791 "net/http" 1792 ) 1793 1794 func main() { 1795 // Simple static webserver: 1796 log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc")))) 1797 } 1798 </textarea></div> 1799 <div class="output"><pre></pre></div> 1800 <div class="buttons"> 1801 <a class="run" title="Run this code [shift-enter]">Run</a> 1802 <a class="fmt" title="Format this code">Format</a> 1803 1804 </div> 1805 </div> 1806 1807 </div> 1808 </div> 1809 <div id="example_FileServer_stripPrefix" class="toggle"> 1810 <div class="collapsed"> 1811 <p class="exampleHeading toggleButton">▹ <span class="text">Example (StripPrefix)</span></p> 1812 </div> 1813 <div class="expanded"> 1814 <p class="exampleHeading toggleButton">▾ <span class="text">Example (StripPrefix)</span></p> 1815 1816 1817 1818 <div class="play"> 1819 <div class="input"><textarea class="code">package main 1820 1821 import ( 1822 "net/http" 1823 ) 1824 1825 func main() { 1826 // To serve a directory on disk (/tmp) under an alternate URL 1827 // path (/tmpfiles/), use StripPrefix to modify the request 1828 // URL's path before the FileServer sees it: 1829 http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))) 1830 } 1831 </textarea></div> 1832 <div class="output"><pre></pre></div> 1833 <div class="buttons"> 1834 <a class="run" title="Run this code [shift-enter]">Run</a> 1835 <a class="fmt" title="Format this code">Format</a> 1836 1837 </div> 1838 </div> 1839 1840 </div> 1841 </div> 1842 1843 1844 1845 1846 <h3 id="NotFoundHandler">func <a href="https://golang.org/src/net/http/server.go?s=42065:42095#L1431">NotFoundHandler</a></h3> 1847 <pre>func NotFoundHandler() <a href="https://golang.org/pkg/net/http/#Handler">Handler</a></pre> 1848 <p> 1849 NotFoundHandler returns a simple request handler 1850 that replies to each request with a “404 page not found” reply. 1851 </p> 1852 1853 1854 1855 1856 1857 <h3 id="RedirectHandler">func <a href="https://golang.org/src/net/http/server.go?s=45249:45299#L1538">RedirectHandler</a></h3> 1858 <pre>func RedirectHandler(url <a href="https://golang.org/pkg/builtin/#string">string</a>, code <a href="https://golang.org/pkg/builtin/#int">int</a>) <a href="https://golang.org/pkg/net/http/#Handler">Handler</a></pre> 1859 <p> 1860 RedirectHandler returns a request handler that redirects 1861 each request it receives to the given url using the given 1862 status code. 1863 </p> 1864 1865 1866 1867 1868 1869 <h3 id="StripPrefix">func <a href="https://golang.org/src/net/http/server.go?s=42404:42454#L1438">StripPrefix</a></h3> 1870 <pre>func StripPrefix(prefix <a href="https://golang.org/pkg/builtin/#string">string</a>, h <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>) <a href="https://golang.org/pkg/net/http/#Handler">Handler</a></pre> 1871 <p> 1872 StripPrefix returns a handler that serves HTTP requests 1873 by removing the given prefix from the request URL's Path 1874 and invoking the handler h. StripPrefix handles a 1875 request for a path that doesn't begin with prefix by 1876 replying with an HTTP 404 not found error. 1877 </p> 1878 1879 <div id="example_StripPrefix" class="toggle"> 1880 <div class="collapsed"> 1881 <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> 1882 </div> 1883 <div class="expanded"> 1884 <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> 1885 1886 1887 1888 <div class="play"> 1889 <div class="input"><textarea class="code">package main 1890 1891 import ( 1892 "net/http" 1893 ) 1894 1895 func main() { 1896 // To serve a directory on disk (/tmp) under an alternate URL 1897 // path (/tmpfiles/), use StripPrefix to modify the request 1898 // URL's path before the FileServer sees it: 1899 http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))) 1900 } 1901 </textarea></div> 1902 <div class="output"><pre></pre></div> 1903 <div class="buttons"> 1904 <a class="run" title="Run this code [shift-enter]">Run</a> 1905 <a class="fmt" title="Format this code">Format</a> 1906 1907 </div> 1908 </div> 1909 1910 </div> 1911 </div> 1912 1913 1914 1915 1916 <h3 id="TimeoutHandler">func <a href="https://golang.org/src/net/http/server.go?s=61275:61343#L2039">TimeoutHandler</a></h3> 1917 <pre>func TimeoutHandler(h <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>, dt <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Duration">Duration</a>, msg <a href="https://golang.org/pkg/builtin/#string">string</a>) <a href="https://golang.org/pkg/net/http/#Handler">Handler</a></pre> 1918 <p> 1919 TimeoutHandler returns a Handler that runs h with the given time limit. 1920 </p> 1921 <p> 1922 The new Handler calls h.ServeHTTP to handle each request, but if a 1923 call runs for longer than its time limit, the handler responds with 1924 a 503 Service Unavailable error and the given message in its body. 1925 (If msg is empty, a suitable default message will be sent.) 1926 After such a timeout, writes by h to its ResponseWriter will return 1927 ErrHandlerTimeout. 1928 </p> 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 <h2 id="HandlerFunc">type <a href="https://golang.org/src/net/http/server.go?s=41267:41314#L1408">HandlerFunc</a></h2> 1939 <pre>type HandlerFunc func(<a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, *<a href="https://golang.org/pkg/net/http/#Request">Request</a>)</pre> 1940 <p> 1941 The HandlerFunc type is an adapter to allow the use of 1942 ordinary functions as HTTP handlers. If f is a function 1943 with the appropriate signature, HandlerFunc(f) is a 1944 Handler object that calls f. 1945 </p> 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 <h3 id="HandlerFunc.ServeHTTP">func (HandlerFunc) <a href="https://golang.org/src/net/http/server.go?s=41344:41404#L1411">ServeHTTP</a></h3> 1961 <pre>func (f <a href="https://golang.org/pkg/net/http/#HandlerFunc">HandlerFunc</a>) ServeHTTP(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>)</pre> 1962 <p> 1963 ServeHTTP calls f(w, r). 1964 </p> 1965 1966 1967 1968 1969 1970 1971 1972 1973 <h2 id="Header">type <a href="https://golang.org/src/net/http/header.go?s=350:381#L9">Header</a></h2> 1974 <pre>type Header map[<a href="https://golang.org/pkg/builtin/#string">string</a>][]<a href="https://golang.org/pkg/builtin/#string">string</a></pre> 1975 <p> 1976 A Header represents the key-value pairs in an HTTP header. 1977 </p> 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 <h3 id="Header.Add">func (Header) <a href="https://golang.org/src/net/http/header.go?s=488:526#L13">Add</a></h3> 1993 <pre>func (h <a href="https://golang.org/pkg/net/http/#Header">Header</a>) Add(key, value <a href="https://golang.org/pkg/builtin/#string">string</a>)</pre> 1994 <p> 1995 Add adds the key, value pair to the header. 1996 It appends to any existing values associated with key. 1997 </p> 1998 1999 2000 2001 2002 2003 2004 <h3 id="Header.Del">func (Header) <a href="https://golang.org/src/net/http/header.go?s=1321:1352#L41">Del</a></h3> 2005 <pre>func (h <a href="https://golang.org/pkg/net/http/#Header">Header</a>) Del(key <a href="https://golang.org/pkg/builtin/#string">string</a>)</pre> 2006 <p> 2007 Del deletes the values associated with key. 2008 </p> 2009 2010 2011 2012 2013 2014 2015 <h3 id="Header.Get">func (Header) <a href="https://golang.org/src/net/http/header.go?s=1015:1053#L28">Get</a></h3> 2016 <pre>func (h <a href="https://golang.org/pkg/net/http/#Header">Header</a>) Get(key <a href="https://golang.org/pkg/builtin/#string">string</a>) <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 2017 <p> 2018 Get gets the first value associated with the given key. 2019 If there are no values associated with the key, Get returns "". 2020 To access multiple values of a key, access the map directly 2021 with CanonicalHeaderKey. 2022 </p> 2023 2024 2025 2026 2027 2028 2029 <h3 id="Header.Set">func (Header) <a href="https://golang.org/src/net/http/header.go?s=713:751#L20">Set</a></h3> 2030 <pre>func (h <a href="https://golang.org/pkg/net/http/#Header">Header</a>) Set(key, value <a href="https://golang.org/pkg/builtin/#string">string</a>)</pre> 2031 <p> 2032 Set sets the header entries associated with key to 2033 the single element value. It replaces any existing 2034 values associated with key. 2035 </p> 2036 2037 2038 2039 2040 2041 2042 <h3 id="Header.Write">func (Header) <a href="https://golang.org/src/net/http/header.go?s=1433:1473#L46">Write</a></h3> 2043 <pre>func (h <a href="https://golang.org/pkg/net/http/#Header">Header</a>) Write(w <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Writer">Writer</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 2044 <p> 2045 Write writes a header in wire format. 2046 </p> 2047 2048 2049 2050 2051 2052 2053 <h3 id="Header.WriteSubset">func (Header) <a href="https://golang.org/src/net/http/header.go?s=3676:3747#L135">WriteSubset</a></h3> 2054 <pre>func (h <a href="https://golang.org/pkg/net/http/#Header">Header</a>) WriteSubset(w <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Writer">Writer</a>, exclude map[<a href="https://golang.org/pkg/builtin/#string">string</a>]<a href="https://golang.org/pkg/builtin/#bool">bool</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 2055 <p> 2056 WriteSubset writes a header in wire format. 2057 If exclude is not nil, keys where exclude[key] == true are not written. 2058 </p> 2059 2060 2061 2062 2063 2064 2065 2066 2067 <h2 id="Hijacker">type <a href="https://golang.org/src/net/http/server.go?s=3126:3656#L86">Hijacker</a></h2> 2068 <pre>type Hijacker interface { 2069 <span class="comment">// Hijack lets the caller take over the connection.</span> 2070 <span class="comment">// After a call to Hijack(), the HTTP server library</span> 2071 <span class="comment">// will not do anything else with the connection.</span> 2072 <span class="comment">//</span> 2073 <span class="comment">// It becomes the caller's responsibility to manage</span> 2074 <span class="comment">// and close the connection.</span> 2075 <span class="comment">//</span> 2076 <span class="comment">// The returned net.Conn may have read or write deadlines</span> 2077 <span class="comment">// already set, depending on the configuration of the</span> 2078 <span class="comment">// Server. It is the caller's responsibility to set</span> 2079 <span class="comment">// or clear those deadlines as needed.</span> 2080 Hijack() (<a href="https://golang.org/pkg/net/">net</a>.<a href="https://golang.org/pkg/net/#Conn">Conn</a>, *<a href="https://golang.org/pkg/bufio/">bufio</a>.<a href="https://golang.org/pkg/bufio/#ReadWriter">ReadWriter</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 2081 }</pre> 2082 <p> 2083 The Hijacker interface is implemented by ResponseWriters that allow 2084 an HTTP handler to take over the connection. 2085 </p> 2086 2087 2088 2089 2090 2091 2092 <div id="example_Hijacker" class="toggle"> 2093 <div class="collapsed"> 2094 <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> 2095 </div> 2096 <div class="expanded"> 2097 <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> 2098 2099 2100 2101 <div class="play"> 2102 <div class="input"><textarea class="code">package main 2103 2104 import ( 2105 "fmt" 2106 "log" 2107 "net/http" 2108 ) 2109 2110 func main() { 2111 http.HandleFunc("/hijack", func(w http.ResponseWriter, r *http.Request) { 2112 hj, ok := w.(http.Hijacker) 2113 if !ok { 2114 http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) 2115 return 2116 } 2117 conn, bufrw, err := hj.Hijack() 2118 if err != nil { 2119 http.Error(w, err.Error(), http.StatusInternalServerError) 2120 return 2121 } 2122 // Don't forget to close the connection: 2123 defer conn.Close() 2124 bufrw.WriteString("Now we're speaking raw TCP. Say hi: ") 2125 bufrw.Flush() 2126 s, err := bufrw.ReadString('\n') 2127 if err != nil { 2128 log.Printf("error reading string: %v", err) 2129 return 2130 } 2131 fmt.Fprintf(bufrw, "You said: %q\nBye.\n", s) 2132 bufrw.Flush() 2133 }) 2134 } 2135 </textarea></div> 2136 <div class="output"><pre></pre></div> 2137 <div class="buttons"> 2138 <a class="run" title="Run this code [shift-enter]">Run</a> 2139 <a class="fmt" title="Format this code">Format</a> 2140 2141 </div> 2142 </div> 2143 2144 </div> 2145 </div> 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 <h2 id="ProtocolError">type <a href="https://golang.org/src/net/http/request.go?s=668:717#L26">ProtocolError</a></h2> 2157 <pre>type ProtocolError struct { 2158 ErrorString <a href="https://golang.org/pkg/builtin/#string">string</a> 2159 }</pre> 2160 <p> 2161 HTTP request parsing errors. 2162 </p> 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 <h3 id="ProtocolError.Error">func (*ProtocolError) <a href="https://golang.org/src/net/http/request.go?s=719:759#L30">Error</a></h3> 2178 <pre>func (err *<a href="https://golang.org/pkg/net/http/#ProtocolError">ProtocolError</a>) Error() <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 2179 2180 2181 2182 2183 2184 2185 2186 2187 <h2 id="Request">type <a href="https://golang.org/src/net/http/request.go?s=2057:8270#L64">Request</a></h2> 2188 <pre>type Request struct { 2189 <span class="comment">// Method specifies the HTTP method (GET, POST, PUT, etc.).</span> 2190 <span class="comment">// For client requests an empty string means GET.</span> 2191 Method <a href="https://golang.org/pkg/builtin/#string">string</a> 2192 2193 <span class="comment">// URL specifies either the URI being requested (for server</span> 2194 <span class="comment">// requests) or the URL to access (for client requests).</span> 2195 <span class="comment">//</span> 2196 <span class="comment">// For server requests the URL is parsed from the URI</span> 2197 <span class="comment">// supplied on the Request-Line as stored in RequestURI. For</span> 2198 <span class="comment">// most requests, fields other than Path and RawQuery will be</span> 2199 <span class="comment">// empty. (See RFC 2616, Section 5.1.2)</span> 2200 <span class="comment">//</span> 2201 <span class="comment">// For client requests, the URL's Host specifies the server to</span> 2202 <span class="comment">// connect to, while the Request's Host field optionally</span> 2203 <span class="comment">// specifies the Host header value to send in the HTTP</span> 2204 <span class="comment">// request.</span> 2205 URL *<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a> 2206 2207 <span class="comment">// The protocol version for incoming requests.</span> 2208 <span class="comment">// Client requests always use HTTP/1.1.</span> 2209 Proto <a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// "HTTP/1.0"</span> 2210 ProtoMajor <a href="https://golang.org/pkg/builtin/#int">int</a> <span class="comment">// 1</span> 2211 ProtoMinor <a href="https://golang.org/pkg/builtin/#int">int</a> <span class="comment">// 0</span> 2212 2213 <span class="comment">// A header maps request lines to their values.</span> 2214 <span class="comment">// If the header says</span> 2215 <span class="comment">//</span> 2216 <span class="comment">// accept-encoding: gzip, deflate</span> 2217 <span class="comment">// Accept-Language: en-us</span> 2218 <span class="comment">// Connection: keep-alive</span> 2219 <span class="comment">//</span> 2220 <span class="comment">// then</span> 2221 <span class="comment">//</span> 2222 <span class="comment">// Header = map[string][]string{</span> 2223 <span class="comment">// "Accept-Encoding": {"gzip, deflate"},</span> 2224 <span class="comment">// "Accept-Language": {"en-us"},</span> 2225 <span class="comment">// "Connection": {"keep-alive"},</span> 2226 <span class="comment">// }</span> 2227 <span class="comment">//</span> 2228 <span class="comment">// HTTP defines that header names are case-insensitive.</span> 2229 <span class="comment">// The request parser implements this by canonicalizing the</span> 2230 <span class="comment">// name, making the first character and any characters</span> 2231 <span class="comment">// following a hyphen uppercase and the rest lowercase.</span> 2232 <span class="comment">//</span> 2233 <span class="comment">// For client requests certain headers are automatically</span> 2234 <span class="comment">// added and may override values in Header.</span> 2235 <span class="comment">//</span> 2236 <span class="comment">// See the documentation for the Request.Write method.</span> 2237 Header <a href="https://golang.org/pkg/net/http/#Header">Header</a> 2238 2239 <span class="comment">// Body is the request's body.</span> 2240 <span class="comment">//</span> 2241 <span class="comment">// For client requests a nil body means the request has no</span> 2242 <span class="comment">// body, such as a GET request. The HTTP Client's Transport</span> 2243 <span class="comment">// is responsible for calling the Close method.</span> 2244 <span class="comment">//</span> 2245 <span class="comment">// For server requests the Request Body is always non-nil</span> 2246 <span class="comment">// but will return EOF immediately when no body is present.</span> 2247 <span class="comment">// The Server will close the request body. The ServeHTTP</span> 2248 <span class="comment">// Handler does not need to.</span> 2249 Body <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#ReadCloser">ReadCloser</a> 2250 2251 <span class="comment">// ContentLength records the length of the associated content.</span> 2252 <span class="comment">// The value -1 indicates that the length is unknown.</span> 2253 <span class="comment">// Values >= 0 indicate that the given number of bytes may</span> 2254 <span class="comment">// be read from Body.</span> 2255 <span class="comment">// For client requests, a value of 0 means unknown if Body is not nil.</span> 2256 ContentLength <a href="https://golang.org/pkg/builtin/#int64">int64</a> 2257 2258 <span class="comment">// TransferEncoding lists the transfer encodings from outermost to</span> 2259 <span class="comment">// innermost. An empty list denotes the "identity" encoding.</span> 2260 <span class="comment">// TransferEncoding can usually be ignored; chunked encoding is</span> 2261 <span class="comment">// automatically added and removed as necessary when sending and</span> 2262 <span class="comment">// receiving requests.</span> 2263 TransferEncoding []<a href="https://golang.org/pkg/builtin/#string">string</a> 2264 2265 <span class="comment">// Close indicates whether to close the connection after</span> 2266 <span class="comment">// replying to this request (for servers) or after sending</span> 2267 <span class="comment">// the request (for clients).</span> 2268 Close <a href="https://golang.org/pkg/builtin/#bool">bool</a> 2269 2270 <span class="comment">// For server requests Host specifies the host on which the</span> 2271 <span class="comment">// URL is sought. Per RFC 2616, this is either the value of</span> 2272 <span class="comment">// the "Host" header or the host name given in the URL itself.</span> 2273 <span class="comment">// It may be of the form "host:port".</span> 2274 <span class="comment">//</span> 2275 <span class="comment">// For client requests Host optionally overrides the Host</span> 2276 <span class="comment">// header to send. If empty, the Request.Write method uses</span> 2277 <span class="comment">// the value of URL.Host.</span> 2278 Host <a href="https://golang.org/pkg/builtin/#string">string</a> 2279 2280 <span class="comment">// Form contains the parsed form data, including both the URL</span> 2281 <span class="comment">// field's query parameters and the POST or PUT form data.</span> 2282 <span class="comment">// This field is only available after ParseForm is called.</span> 2283 <span class="comment">// The HTTP client ignores Form and uses Body instead.</span> 2284 Form <a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#Values">Values</a> 2285 2286 <span class="comment">// PostForm contains the parsed form data from POST, PATCH,</span> 2287 <span class="comment">// or PUT body parameters.</span> 2288 <span class="comment">//</span> 2289 <span class="comment">// This field is only available after ParseForm is called.</span> 2290 <span class="comment">// The HTTP client ignores PostForm and uses Body instead.</span> 2291 PostForm <a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#Values">Values</a> 2292 2293 <span class="comment">// MultipartForm is the parsed multipart form, including file uploads.</span> 2294 <span class="comment">// This field is only available after ParseMultipartForm is called.</span> 2295 <span class="comment">// The HTTP client ignores MultipartForm and uses Body instead.</span> 2296 MultipartForm *<a href="https://golang.org/pkg/mime/multipart/">multipart</a>.<a href="https://golang.org/pkg/mime/multipart/#Form">Form</a> 2297 2298 <span class="comment">// Trailer specifies additional headers that are sent after the request</span> 2299 <span class="comment">// body.</span> 2300 <span class="comment">//</span> 2301 <span class="comment">// For server requests the Trailer map initially contains only the</span> 2302 <span class="comment">// trailer keys, with nil values. (The client declares which trailers it</span> 2303 <span class="comment">// will later send.) While the handler is reading from Body, it must</span> 2304 <span class="comment">// not reference Trailer. After reading from Body returns EOF, Trailer</span> 2305 <span class="comment">// can be read again and will contain non-nil values, if they were sent</span> 2306 <span class="comment">// by the client.</span> 2307 <span class="comment">//</span> 2308 <span class="comment">// For client requests Trailer must be initialized to a map containing</span> 2309 <span class="comment">// the trailer keys to later send. The values may be nil or their final</span> 2310 <span class="comment">// values. The ContentLength must be 0 or -1, to send a chunked request.</span> 2311 <span class="comment">// After the HTTP request is sent the map values can be updated while</span> 2312 <span class="comment">// the request body is read. Once the body returns EOF, the caller must</span> 2313 <span class="comment">// not mutate Trailer.</span> 2314 <span class="comment">//</span> 2315 <span class="comment">// Few HTTP clients, servers, or proxies support HTTP trailers.</span> 2316 Trailer <a href="https://golang.org/pkg/net/http/#Header">Header</a> 2317 2318 <span class="comment">// RemoteAddr allows HTTP servers and other software to record</span> 2319 <span class="comment">// the network address that sent the request, usually for</span> 2320 <span class="comment">// logging. This field is not filled in by ReadRequest and</span> 2321 <span class="comment">// has no defined format. The HTTP server in this package</span> 2322 <span class="comment">// sets RemoteAddr to an "IP:port" address before invoking a</span> 2323 <span class="comment">// handler.</span> 2324 <span class="comment">// This field is ignored by the HTTP client.</span> 2325 RemoteAddr <a href="https://golang.org/pkg/builtin/#string">string</a> 2326 2327 <span class="comment">// RequestURI is the unmodified Request-URI of the</span> 2328 <span class="comment">// Request-Line (RFC 2616, Section 5.1) as sent by the client</span> 2329 <span class="comment">// to a server. Usually the URL field should be used instead.</span> 2330 <span class="comment">// It is an error to set this field in an HTTP client request.</span> 2331 RequestURI <a href="https://golang.org/pkg/builtin/#string">string</a> 2332 2333 <span class="comment">// TLS allows HTTP servers and other software to record</span> 2334 <span class="comment">// information about the TLS connection on which the request</span> 2335 <span class="comment">// was received. This field is not filled in by ReadRequest.</span> 2336 <span class="comment">// The HTTP server in this package sets the field for</span> 2337 <span class="comment">// TLS-enabled connections before invoking a handler;</span> 2338 <span class="comment">// otherwise it leaves the field nil.</span> 2339 <span class="comment">// This field is ignored by the HTTP client.</span> 2340 TLS *<a href="https://golang.org/pkg/crypto/tls/">tls</a>.<a href="https://golang.org/pkg/crypto/tls/#ConnectionState">ConnectionState</a> 2341 2342 <span class="comment">// Cancel is an optional channel whose closure indicates that the client</span> 2343 <span class="comment">// request should be regarded as canceled. Not all implementations of</span> 2344 <span class="comment">// RoundTripper may support Cancel.</span> 2345 <span class="comment">//</span> 2346 <span class="comment">// For server requests, this field is not applicable.</span> 2347 Cancel <-chan struct{} 2348 }</pre> 2349 <p> 2350 A Request represents an HTTP request received by a server 2351 or to be sent by a client. 2352 </p> 2353 <p> 2354 The field semantics differ slightly between client and server 2355 usage. In addition to the notes on the fields below, see the 2356 documentation for Request.Write and RoundTripper. 2357 </p> 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 <h3 id="NewRequest">func <a href="https://golang.org/src/net/http/request.go?s=17718:17790#L536">NewRequest</a></h3> 2371 <pre>func NewRequest(method, urlStr <a href="https://golang.org/pkg/builtin/#string">string</a>, body <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Reader">Reader</a>) (*<a href="https://golang.org/pkg/net/http/#Request">Request</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2372 <p> 2373 NewRequest returns a new Request given a method, URL, and optional body. 2374 </p> 2375 <p> 2376 If the provided body is also an io.Closer, the returned 2377 Request.Body is set to body and will be closed by the Client 2378 methods Do, Post, and PostForm, and Transport.RoundTrip. 2379 </p> 2380 <p> 2381 NewRequest returns a Request suitable for use with Client.Do or 2382 Transport.RoundTrip. 2383 To create a request for use with testing a Server Handler use either 2384 ReadRequest or manually update the Request fields. See the Request 2385 type's documentation for the difference between inbound and outbound 2386 request fields. 2387 </p> 2388 2389 2390 2391 2392 2393 <h3 id="ReadRequest">func <a href="https://golang.org/src/net/http/request.go?s=20301:20360#L636">ReadRequest</a></h3> 2394 <pre>func ReadRequest(b *<a href="https://golang.org/pkg/bufio/">bufio</a>.<a href="https://golang.org/pkg/bufio/#Reader">Reader</a>) (req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2395 <p> 2396 ReadRequest reads and parses an incoming request from b. 2397 </p> 2398 2399 2400 2401 2402 2403 2404 2405 <h3 id="Request.AddCookie">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=9423:9461#L259">AddCookie</a></h3> 2406 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) AddCookie(c *<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a>)</pre> 2407 <p> 2408 AddCookie adds a cookie to the request. Per RFC 6265 section 5.4, 2409 AddCookie does not attach more than one Cookie header field. That 2410 means all cookies, if any, are written into the same line, 2411 separated by semicolon. 2412 </p> 2413 2414 2415 2416 2417 2418 2419 <h3 id="Request.BasicAuth">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=18567:18633#L572">BasicAuth</a></h3> 2420 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) BasicAuth() (username, password <a href="https://golang.org/pkg/builtin/#string">string</a>, ok <a href="https://golang.org/pkg/builtin/#bool">bool</a>)</pre> 2421 <p> 2422 BasicAuth returns the username and password provided in the request's 2423 Authorization header, if the request uses HTTP Basic Authentication. 2424 See RFC 2617, Section 2. 2425 </p> 2426 2427 2428 2429 2430 2431 2432 <h3 id="Request.Cookie">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=9041:9095#L248">Cookie</a></h3> 2433 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) Cookie(name <a href="https://golang.org/pkg/builtin/#string">string</a>) (*<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2434 <p> 2435 Cookie returns the named cookie provided in the request or 2436 ErrNoCookie if not found. 2437 </p> 2438 2439 2440 2441 2442 2443 2444 <h3 id="Request.Cookies">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=8727:8764#L239">Cookies</a></h3> 2445 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) Cookies() []*<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a></pre> 2446 <p> 2447 Cookies parses and returns the HTTP cookies sent with the request. 2448 </p> 2449 2450 2451 2452 2453 2454 2455 <h3 id="Request.FormFile">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=29076:29161#L960">FormFile</a></h3> 2456 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) FormFile(key <a href="https://golang.org/pkg/builtin/#string">string</a>) (<a href="https://golang.org/pkg/mime/multipart/">multipart</a>.<a href="https://golang.org/pkg/mime/multipart/#File">File</a>, *<a href="https://golang.org/pkg/mime/multipart/">multipart</a>.<a href="https://golang.org/pkg/mime/multipart/#FileHeader">FileHeader</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2457 <p> 2458 FormFile returns the first file for the provided form key. 2459 FormFile calls ParseMultipartForm and ParseForm if necessary. 2460 </p> 2461 2462 2463 2464 2465 2466 2467 <h3 id="Request.FormValue">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=28248:28294#L933">FormValue</a></h3> 2468 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) FormValue(key <a href="https://golang.org/pkg/builtin/#string">string</a>) <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 2469 <p> 2470 FormValue returns the first value for the named component of the query. 2471 POST and PUT body parameters take precedence over URL query string values. 2472 FormValue calls ParseMultipartForm and ParseForm if necessary and ignores 2473 any errors returned by these functions. 2474 If key is not present, FormValue returns the empty string. 2475 To access multiple values of the same key, call ParseForm and 2476 then inspect Request.Form directly. 2477 </p> 2478 2479 2480 2481 2482 2483 2484 <h3 id="Request.MultipartReader">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=10760:10822#L292">MultipartReader</a></h3> 2485 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) MultipartReader() (*<a href="https://golang.org/pkg/mime/multipart/">multipart</a>.<a href="https://golang.org/pkg/mime/multipart/#Reader">Reader</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2486 <p> 2487 MultipartReader returns a MIME multipart reader if this is a 2488 multipart/form-data POST request, else returns nil and an error. 2489 Use this function instead of ParseMultipartForm to 2490 process the request body as a stream. 2491 </p> 2492 2493 2494 2495 2496 2497 2498 <h3 id="Request.ParseForm">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=26212:26247#L854">ParseForm</a></h3> 2499 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) ParseForm() <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 2500 <p> 2501 ParseForm parses the raw query from the URL and updates r.Form. 2502 </p> 2503 <p> 2504 For POST or PUT requests, it also parses the request body as a form and 2505 put the results into both r.PostForm and r.Form. 2506 POST and PUT body parameters take precedence over URL query string values 2507 in r.Form. 2508 </p> 2509 <p> 2510 If the request Body's size has not already been limited by MaxBytesReader, 2511 the size is capped at 10MB. 2512 </p> 2513 <p> 2514 ParseMultipartForm calls ParseForm automatically. 2515 It is idempotent. 2516 </p> 2517 2518 2519 2520 2521 2522 2523 <h3 id="Request.ParseMultipartForm">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=27267:27326#L895">ParseMultipartForm</a></h3> 2524 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) ParseMultipartForm(maxMemory <a href="https://golang.org/pkg/builtin/#int64">int64</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 2525 <p> 2526 ParseMultipartForm parses a request body as multipart/form-data. 2527 The whole request body is parsed and up to a total of maxMemory bytes of 2528 its file parts are stored in memory, with the remainder stored on 2529 disk in temporary files. 2530 ParseMultipartForm calls ParseForm if necessary. 2531 After one call to ParseMultipartForm, subsequent calls have no effect. 2532 </p> 2533 2534 2535 2536 2537 2538 2539 <h3 id="Request.PostFormValue">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=28755:28805#L948">PostFormValue</a></h3> 2540 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) PostFormValue(key <a href="https://golang.org/pkg/builtin/#string">string</a>) <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 2541 <p> 2542 PostFormValue returns the first value for the named component of the POST 2543 or PUT request body. URL query parameters are ignored. 2544 PostFormValue calls ParseMultipartForm and ParseForm if necessary and ignores 2545 any errors returned by these functions. 2546 If key is not present, PostFormValue returns the empty string. 2547 </p> 2548 2549 2550 2551 2552 2553 2554 <h3 id="Request.ProtoAtLeast">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=8370:8423#L228">ProtoAtLeast</a></h3> 2555 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) ProtoAtLeast(major, minor <a href="https://golang.org/pkg/builtin/#int">int</a>) <a href="https://golang.org/pkg/builtin/#bool">bool</a></pre> 2556 <p> 2557 ProtoAtLeast reports whether the HTTP protocol used 2558 in the request is at least major.minor. 2559 </p> 2560 2561 2562 2563 2564 2565 2566 <h3 id="Request.Referer">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=10131:10165#L276">Referer</a></h3> 2567 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) Referer() <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 2568 <p> 2569 Referer returns the referring URL, if sent in the request. 2570 </p> 2571 <p> 2572 Referer is misspelled as in the request itself, a mistake from the 2573 earliest days of HTTP. This value can also be fetched from the 2574 Header map as Header["Referer"]; the benefit of making it available 2575 as a method is that the compiler can diagnose programs that use the 2576 alternate (correct English) spelling req.Referrer() but cannot 2577 diagnose programs that use Header["Referrer"]. 2578 </p> 2579 2580 2581 2582 2583 2584 2585 <h3 id="Request.SetBasicAuth">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=19455:19512#L604">SetBasicAuth</a></h3> 2586 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) SetBasicAuth(username, password <a href="https://golang.org/pkg/builtin/#string">string</a>)</pre> 2587 <p> 2588 SetBasicAuth sets the request's Authorization header to use HTTP 2589 Basic Authentication with the provided username and password. 2590 </p> 2591 <p> 2592 With HTTP Basic Authentication the provided username and password 2593 are not encrypted. 2594 </p> 2595 2596 2597 2598 2599 2600 2601 <h3 id="Request.UserAgent">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=8580:8616#L234">UserAgent</a></h3> 2602 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) UserAgent() <a href="https://golang.org/pkg/builtin/#string">string</a></pre> 2603 <p> 2604 UserAgent returns the client's User-Agent, if sent in the request. 2605 </p> 2606 2607 2608 2609 2610 2611 2612 <h3 id="Request.Write">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=12411:12453#L346">Write</a></h3> 2613 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) Write(w <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Writer">Writer</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 2614 <p> 2615 Write writes an HTTP/1.1 request, which is the header and body, in wire format. 2616 This method consults the following fields of the request: 2617 </p> 2618 <pre>Host 2619 URL 2620 Method (defaults to "GET") 2621 Header 2622 ContentLength 2623 TransferEncoding 2624 Body 2625 </pre> 2626 <p> 2627 If Body is present, Content-Length is <= 0 and TransferEncoding 2628 hasn't been set to "identity", Write adds "Transfer-Encoding: 2629 chunked" to the header. Body is closed after it is sent. 2630 </p> 2631 2632 2633 2634 2635 2636 2637 <h3 id="Request.WriteProxy">func (*Request) <a href="https://golang.org/src/net/http/request.go?s=12846:12893#L356">WriteProxy</a></h3> 2638 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) WriteProxy(w <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Writer">Writer</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 2639 <p> 2640 WriteProxy is like Write but writes the request in the form 2641 expected by an HTTP proxy. In particular, WriteProxy writes the 2642 initial Request-URI line of the request with an absolute URI, per 2643 section 5.1.2 of RFC 2616, including the scheme and host. 2644 In either case, WriteProxy also writes a Host header, using 2645 either r.Host or r.URL.Host. 2646 </p> 2647 2648 2649 2650 2651 2652 2653 2654 2655 <h2 id="Response">type <a href="https://golang.org/src/net/http/response.go?s=512:2849#L19">Response</a></h2> 2656 <pre>type Response struct { 2657 Status <a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// e.g. "200 OK"</span> 2658 StatusCode <a href="https://golang.org/pkg/builtin/#int">int</a> <span class="comment">// e.g. 200</span> 2659 Proto <a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// e.g. "HTTP/1.0"</span> 2660 ProtoMajor <a href="https://golang.org/pkg/builtin/#int">int</a> <span class="comment">// e.g. 1</span> 2661 ProtoMinor <a href="https://golang.org/pkg/builtin/#int">int</a> <span class="comment">// e.g. 0</span> 2662 2663 <span class="comment">// Header maps header keys to values. If the response had multiple</span> 2664 <span class="comment">// headers with the same key, they may be concatenated, with comma</span> 2665 <span class="comment">// delimiters. (Section 4.2 of RFC 2616 requires that multiple headers</span> 2666 <span class="comment">// be semantically equivalent to a comma-delimited sequence.) Values</span> 2667 <span class="comment">// duplicated by other fields in this struct (e.g., ContentLength) are</span> 2668 <span class="comment">// omitted from Header.</span> 2669 <span class="comment">//</span> 2670 <span class="comment">// Keys in the map are canonicalized (see CanonicalHeaderKey).</span> 2671 Header <a href="https://golang.org/pkg/net/http/#Header">Header</a> 2672 2673 <span class="comment">// Body represents the response body.</span> 2674 <span class="comment">//</span> 2675 <span class="comment">// The http Client and Transport guarantee that Body is always</span> 2676 <span class="comment">// non-nil, even on responses without a body or responses with</span> 2677 <span class="comment">// a zero-length body. It is the caller's responsibility to</span> 2678 <span class="comment">// close Body. The default HTTP client's Transport does not</span> 2679 <span class="comment">// attempt to reuse HTTP/1.0 or HTTP/1.1 TCP connections</span> 2680 <span class="comment">// ("keep-alive") unless the Body is read to completion and is</span> 2681 <span class="comment">// closed.</span> 2682 <span class="comment">//</span> 2683 <span class="comment">// The Body is automatically dechunked if the server replied</span> 2684 <span class="comment">// with a "chunked" Transfer-Encoding.</span> 2685 Body <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#ReadCloser">ReadCloser</a> 2686 2687 <span class="comment">// ContentLength records the length of the associated content. The</span> 2688 <span class="comment">// value -1 indicates that the length is unknown. Unless Request.Method</span> 2689 <span class="comment">// is "HEAD", values >= 0 indicate that the given number of bytes may</span> 2690 <span class="comment">// be read from Body.</span> 2691 ContentLength <a href="https://golang.org/pkg/builtin/#int64">int64</a> 2692 2693 <span class="comment">// Contains transfer encodings from outer-most to inner-most. Value is</span> 2694 <span class="comment">// nil, means that "identity" encoding is used.</span> 2695 TransferEncoding []<a href="https://golang.org/pkg/builtin/#string">string</a> 2696 2697 <span class="comment">// Close records whether the header directed that the connection be</span> 2698 <span class="comment">// closed after reading Body. The value is advice for clients: neither</span> 2699 <span class="comment">// ReadResponse nor Response.Write ever closes a connection.</span> 2700 Close <a href="https://golang.org/pkg/builtin/#bool">bool</a> 2701 2702 <span class="comment">// Trailer maps trailer keys to values, in the same</span> 2703 <span class="comment">// format as the header.</span> 2704 Trailer <a href="https://golang.org/pkg/net/http/#Header">Header</a> 2705 2706 <span class="comment">// The Request that was sent to obtain this Response.</span> 2707 <span class="comment">// Request's Body is nil (having already been consumed).</span> 2708 <span class="comment">// This is only populated for Client requests.</span> 2709 Request *<a href="https://golang.org/pkg/net/http/#Request">Request</a> 2710 2711 <span class="comment">// TLS contains information about the TLS connection on which the</span> 2712 <span class="comment">// response was received. It is nil for unencrypted responses.</span> 2713 <span class="comment">// The pointer is shared between responses and should not be</span> 2714 <span class="comment">// modified.</span> 2715 TLS *<a href="https://golang.org/pkg/crypto/tls/">tls</a>.<a href="https://golang.org/pkg/crypto/tls/#ConnectionState">ConnectionState</a> 2716 }</pre> 2717 <p> 2718 Response represents the response from an HTTP request. 2719 </p> 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 <h3 id="Get">func <a href="https://golang.org/src/net/http/client.go?s=9198:9246#L270">Get</a></h3> 2733 <pre>func Get(url <a href="https://golang.org/pkg/builtin/#string">string</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2734 <p> 2735 Get issues a GET to the specified URL. If the response is one of 2736 the following redirect codes, Get follows the redirect, up to a 2737 maximum of 10 redirects: 2738 </p> 2739 <pre>301 (Moved Permanently) 2740 302 (Found) 2741 303 (See Other) 2742 307 (Temporary Redirect) 2743 </pre> 2744 <p> 2745 An error is returned if there were too many redirects or if there 2746 was an HTTP protocol error. A non-2xx response doesn't cause an 2747 error. 2748 </p> 2749 <p> 2750 When err is nil, resp always contains a non-nil resp.Body. 2751 Caller should close resp.Body when done reading from it. 2752 </p> 2753 <p> 2754 Get is a wrapper around DefaultClient.Get. 2755 </p> 2756 <p> 2757 To make a request with custom headers, use NewRequest and 2758 DefaultClient.Do. 2759 </p> 2760 2761 <div id="example_Get" class="toggle"> 2762 <div class="collapsed"> 2763 <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> 2764 </div> 2765 <div class="expanded"> 2766 <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> 2767 2768 2769 2770 <div class="play"> 2771 <div class="input"><textarea class="code">package main 2772 2773 import ( 2774 "fmt" 2775 "io/ioutil" 2776 "log" 2777 "net/http" 2778 ) 2779 2780 func main() { 2781 res, err := http.Get("http://www.google.com/robots.txt") 2782 if err != nil { 2783 log.Fatal(err) 2784 } 2785 robots, err := ioutil.ReadAll(res.Body) 2786 res.Body.Close() 2787 if err != nil { 2788 log.Fatal(err) 2789 } 2790 fmt.Printf("%s", robots) 2791 } 2792 </textarea></div> 2793 <div class="output"><pre></pre></div> 2794 <div class="buttons"> 2795 <a class="run" title="Run this code [shift-enter]">Run</a> 2796 <a class="fmt" title="Format this code">Format</a> 2797 2798 </div> 2799 </div> 2800 2801 </div> 2802 </div> 2803 2804 2805 2806 2807 <h3 id="Head">func <a href="https://golang.org/src/net/http/client.go?s=15901:15950#L500">Head</a></h3> 2808 <pre>func Head(url <a href="https://golang.org/pkg/builtin/#string">string</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2809 <p> 2810 Head issues a HEAD to the specified URL. If the response is one of 2811 the following redirect codes, Head follows the redirect, up to a 2812 maximum of 10 redirects: 2813 </p> 2814 <pre>301 (Moved Permanently) 2815 302 (Found) 2816 303 (See Other) 2817 307 (Temporary Redirect) 2818 </pre> 2819 <p> 2820 Head is a wrapper around DefaultClient.Head 2821 </p> 2822 2823 2824 2825 2826 2827 <h3 id="Post">func <a href="https://golang.org/src/net/http/client.go?s=13816:13898#L443">Post</a></h3> 2828 <pre>func Post(url <a href="https://golang.org/pkg/builtin/#string">string</a>, bodyType <a href="https://golang.org/pkg/builtin/#string">string</a>, body <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Reader">Reader</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2829 <p> 2830 Post issues a POST to the specified URL. 2831 </p> 2832 <p> 2833 Caller should close resp.Body when done reading from it. 2834 </p> 2835 <p> 2836 If the provided body is an io.Closer, it is closed after the 2837 request. 2838 </p> 2839 <p> 2840 Post is a wrapper around DefaultClient.Post. 2841 </p> 2842 <p> 2843 To set custom headers, use NewRequest and DefaultClient.Do. 2844 </p> 2845 2846 2847 2848 2849 2850 <h3 id="PostForm">func <a href="https://golang.org/src/net/http/client.go?s=14909:14979#L474">PostForm</a></h3> 2851 <pre>func PostForm(url <a href="https://golang.org/pkg/builtin/#string">string</a>, data <a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#Values">Values</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2852 <p> 2853 PostForm issues a POST to the specified URL, with data's keys and 2854 values URL-encoded as the request body. 2855 </p> 2856 <p> 2857 The Content-Type header is set to application/x-www-form-urlencoded. 2858 To set other headers, use NewRequest and DefaultClient.Do. 2859 </p> 2860 <p> 2861 When err is nil, resp always contains a non-nil resp.Body. 2862 Caller should close resp.Body when done reading from it. 2863 </p> 2864 <p> 2865 PostForm is a wrapper around DefaultClient.PostForm. 2866 </p> 2867 2868 2869 2870 2871 2872 <h3 id="ReadResponse">func <a href="https://golang.org/src/net/http/response.go?s=3992:4059#L111">ReadResponse</a></h3> 2873 <pre>func ReadResponse(r *<a href="https://golang.org/pkg/bufio/">bufio</a>.<a href="https://golang.org/pkg/bufio/#Reader">Reader</a>, req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (*<a href="https://golang.org/pkg/net/http/#Response">Response</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2874 <p> 2875 ReadResponse reads and returns an HTTP response from r. 2876 The req parameter optionally specifies the Request that corresponds 2877 to this Response. If nil, a GET request is assumed. 2878 Clients must call resp.Body.Close when finished reading resp.Body. 2879 After that call, clients can inspect resp.Trailer to find key/value 2880 pairs included in the response trailer. 2881 </p> 2882 2883 2884 2885 2886 2887 2888 2889 <h3 id="Response.Cookies">func (*Response) <a href="https://golang.org/src/net/http/response.go?s=2924:2962#L82">Cookies</a></h3> 2890 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Response">Response</a>) Cookies() []*<a href="https://golang.org/pkg/net/http/#Cookie">Cookie</a></pre> 2891 <p> 2892 Cookies parses and returns the cookies set in the Set-Cookie headers. 2893 </p> 2894 2895 2896 2897 2898 2899 2900 <h3 id="Response.Location">func (*Response) <a href="https://golang.org/src/net/http/response.go?s=3387:3434#L94">Location</a></h3> 2901 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Response">Response</a>) Location() (*<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 2902 <p> 2903 Location returns the URL of the response's "Location" header, 2904 if present. Relative redirects are resolved relative to 2905 the Response's Request. ErrNoLocation is returned if no 2906 Location header is present. 2907 </p> 2908 2909 2910 2911 2912 2913 2914 <h3 id="Response.ProtoAtLeast">func (*Response) <a href="https://golang.org/src/net/http/response.go?s=5570:5624#L179">ProtoAtLeast</a></h3> 2915 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Response">Response</a>) ProtoAtLeast(major, minor <a href="https://golang.org/pkg/builtin/#int">int</a>) <a href="https://golang.org/pkg/builtin/#bool">bool</a></pre> 2916 <p> 2917 ProtoAtLeast reports whether the HTTP protocol used 2918 in the response is at least major.minor. 2919 </p> 2920 2921 2922 2923 2924 2925 2926 <h3 id="Response.Write">func (*Response) <a href="https://golang.org/src/net/http/response.go?s=6162:6205#L200">Write</a></h3> 2927 <pre>func (r *<a href="https://golang.org/pkg/net/http/#Response">Response</a>) Write(w <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Writer">Writer</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 2928 <p> 2929 Write writes r to w in the HTTP/1.n server response format, 2930 including the status line, headers, body, and optional trailer. 2931 </p> 2932 <p> 2933 This method consults the following fields of the response r: 2934 </p> 2935 <pre>StatusCode 2936 ProtoMajor 2937 ProtoMinor 2938 Request.Method 2939 TransferEncoding 2940 Trailer 2941 Body 2942 ContentLength 2943 Header, values for non-canonical keys will have unpredictable behavior 2944 </pre> 2945 <p> 2946 The Response Body is closed after it is sent. 2947 </p> 2948 2949 2950 2951 2952 2953 2954 2955 2956 <h2 id="ResponseWriter">type <a href="https://golang.org/src/net/http/server.go?s=1517:2599#L48">ResponseWriter</a></h2> 2957 <pre>type ResponseWriter interface { 2958 <span class="comment">// Header returns the header map that will be sent by</span> 2959 <span class="comment">// WriteHeader. Changing the header after a call to</span> 2960 <span class="comment">// WriteHeader (or Write) has no effect unless the modified</span> 2961 <span class="comment">// headers were declared as trailers by setting the</span> 2962 <span class="comment">// "Trailer" header before the call to WriteHeader (see example).</span> 2963 <span class="comment">// To suppress implicit response headers, set their value to nil.</span> 2964 Header() <a href="https://golang.org/pkg/net/http/#Header">Header</a> 2965 2966 <span class="comment">// Write writes the data to the connection as part of an HTTP reply.</span> 2967 <span class="comment">// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)</span> 2968 <span class="comment">// before writing the data. If the Header does not contain a</span> 2969 <span class="comment">// Content-Type line, Write adds a Content-Type set to the result of passing</span> 2970 <span class="comment">// the initial 512 bytes of written data to DetectContentType.</span> 2971 Write([]<a href="https://golang.org/pkg/builtin/#byte">byte</a>) (<a href="https://golang.org/pkg/builtin/#int">int</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 2972 2973 <span class="comment">// WriteHeader sends an HTTP response header with status code.</span> 2974 <span class="comment">// If WriteHeader is not called explicitly, the first call to Write</span> 2975 <span class="comment">// will trigger an implicit WriteHeader(http.StatusOK).</span> 2976 <span class="comment">// Thus explicit calls to WriteHeader are mainly used to</span> 2977 <span class="comment">// send error codes.</span> 2978 WriteHeader(<a href="https://golang.org/pkg/builtin/#int">int</a>) 2979 }</pre> 2980 <p> 2981 A ResponseWriter interface is used by an HTTP handler to 2982 construct an HTTP response. 2983 </p> 2984 2985 2986 2987 2988 2989 2990 <div id="example_ResponseWriter_trailers" class="toggle"> 2991 <div class="collapsed"> 2992 <p class="exampleHeading toggleButton">▹ <span class="text">Example (Trailers)</span></p> 2993 </div> 2994 <div class="expanded"> 2995 <p class="exampleHeading toggleButton">▾ <span class="text">Example (Trailers)</span></p> 2996 <p>HTTP Trailers are a set of key/value pairs like headers that come 2997 after the HTTP response, instead of before. 2998 </p> 2999 3000 3001 <div class="play"> 3002 <div class="input"><textarea class="code">package main 3003 3004 import ( 3005 "io" 3006 "net/http" 3007 ) 3008 3009 func main() { 3010 mux := http.NewServeMux() 3011 mux.HandleFunc("/sendstrailers", func(w http.ResponseWriter, req *http.Request) { 3012 // Before any call to WriteHeader or Write, declare 3013 // the trailers you will set during the HTTP 3014 // response. These three headers are actually sent in 3015 // the trailer. 3016 w.Header().Set("Trailer", "AtEnd1, AtEnd2") 3017 w.Header().Add("Trailer", "AtEnd3") 3018 3019 w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header 3020 w.WriteHeader(http.StatusOK) 3021 3022 w.Header().Set("AtEnd1", "value 1") 3023 io.WriteString(w, "This HTTP response has both headers before this text and trailers at the end.\n") 3024 w.Header().Set("AtEnd2", "value 2") 3025 w.Header().Set("AtEnd3", "value 3") // These will appear as trailers. 3026 }) 3027 } 3028 </textarea></div> 3029 <div class="output"><pre></pre></div> 3030 <div class="buttons"> 3031 <a class="run" title="Run this code [shift-enter]">Run</a> 3032 <a class="fmt" title="Format this code">Format</a> 3033 3034 </div> 3035 </div> 3036 3037 </div> 3038 </div> 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 <h2 id="RoundTripper">type <a href="https://golang.org/src/net/http/client.go?s=2750:3517#L73">RoundTripper</a></h2> 3050 <pre>type RoundTripper interface { 3051 <span class="comment">// RoundTrip executes a single HTTP transaction, returning</span> 3052 <span class="comment">// the Response for the request req. RoundTrip should not</span> 3053 <span class="comment">// attempt to interpret the response. In particular,</span> 3054 <span class="comment">// RoundTrip must return err == nil if it obtained a response,</span> 3055 <span class="comment">// regardless of the response's HTTP status code. A non-nil</span> 3056 <span class="comment">// err should be reserved for failure to obtain a response.</span> 3057 <span class="comment">// Similarly, RoundTrip should not attempt to handle</span> 3058 <span class="comment">// higher-level protocol details such as redirects,</span> 3059 <span class="comment">// authentication, or cookies.</span> 3060 <span class="comment">//</span> 3061 <span class="comment">// RoundTrip should not modify the request, except for</span> 3062 <span class="comment">// consuming and closing the Body, including on errors. The</span> 3063 <span class="comment">// request's URL and Header fields are guaranteed to be</span> 3064 <span class="comment">// initialized.</span> 3065 RoundTrip(*<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (*<a href="https://golang.org/pkg/net/http/#Response">Response</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 3066 }</pre> 3067 <p> 3068 RoundTripper is an interface representing the ability to execute a 3069 single HTTP transaction, obtaining the Response for a given Request. 3070 </p> 3071 <p> 3072 A RoundTripper must be safe for concurrent use by multiple 3073 goroutines. 3074 </p> 3075 3076 3077 3078 3079 3080 <pre>var <span id="DefaultTransport">DefaultTransport</span> <a href="https://golang.org/pkg/net/http/#RoundTripper">RoundTripper</a> = &<a href="https://golang.org/pkg/net/http/#Transport">Transport</a>{ 3081 <a href="https://golang.org/pkg/net/http/#Proxy">Proxy</a>: <a href="https://golang.org/pkg/net/http/#ProxyFromEnvironment">ProxyFromEnvironment</a>, 3082 <a href="https://golang.org/pkg/net/http/#Dial">Dial</a>: (&<a href="https://golang.org/pkg/net/">net</a>.<a href="https://golang.org/pkg/net/#Dialer">Dialer</a>{ 3083 <a href="https://golang.org/pkg/net/http/#Timeout">Timeout</a>: 30 * <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Second">Second</a>, 3084 <a href="https://golang.org/pkg/net/http/#KeepAlive">KeepAlive</a>: 30 * <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Second">Second</a>, 3085 }).<a href="https://golang.org/pkg/net/http/#Dial">Dial</a>, 3086 <a href="https://golang.org/pkg/net/http/#TLSHandshakeTimeout">TLSHandshakeTimeout</a>: 10 * <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Second">Second</a>, 3087 }</pre> 3088 <p> 3089 DefaultTransport is the default implementation of Transport and is 3090 used by DefaultClient. It establishes network connections as needed 3091 and caches them for reuse by subsequent calls. It uses HTTP proxies 3092 as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and 3093 $no_proxy) environment variables. 3094 </p> 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 <h3 id="NewFileTransport">func <a href="https://golang.org/src/net/http/filetransport.go?s=827:876#L20">NewFileTransport</a></h3> 3105 <pre>func NewFileTransport(fs <a href="https://golang.org/pkg/net/http/#FileSystem">FileSystem</a>) <a href="https://golang.org/pkg/net/http/#RoundTripper">RoundTripper</a></pre> 3106 <p> 3107 NewFileTransport returns a new RoundTripper, serving the provided 3108 FileSystem. The returned RoundTripper ignores the URL host in its 3109 incoming requests, as well as most other properties of the 3110 request. 3111 </p> 3112 <p> 3113 The typical use case for NewFileTransport is to register the "file" 3114 protocol with a Transport, as in: 3115 </p> 3116 <pre>t := &http.Transport{} 3117 t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/"))) 3118 c := &http.Client{Transport: t} 3119 res, err := c.Get("file:///etc/passwd") 3120 ... 3121 </pre> 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 <h2 id="ServeMux">type <a href="https://golang.org/src/net/http/server.go?s=46684:46809#L1569">ServeMux</a></h2> 3132 <pre>type ServeMux struct { 3133 <span class="comment">// contains filtered or unexported fields</span> 3134 }</pre> 3135 <p> 3136 ServeMux is an HTTP request multiplexer. 3137 It matches the URL of each incoming request against a list of registered 3138 patterns and calls the handler for the pattern that 3139 most closely matches the URL. 3140 </p> 3141 <p> 3142 Patterns name fixed, rooted paths, like "/favicon.ico", 3143 or rooted subtrees, like "/images/" (note the trailing slash). 3144 Longer patterns take precedence over shorter ones, so that 3145 if there are handlers registered for both "/images/" 3146 and "/images/thumbnails/", the latter handler will be 3147 called for paths beginning "/images/thumbnails/" and the 3148 former will receive requests for any other paths in the 3149 "/images/" subtree. 3150 </p> 3151 <p> 3152 Note that since a pattern ending in a slash names a rooted subtree, 3153 the pattern "/" matches all paths not matched by other registered 3154 patterns, not just the URL with Path == "/". 3155 </p> 3156 <p> 3157 Patterns may optionally begin with a host name, restricting matches to 3158 URLs on that host only. Host-specific patterns take precedence over 3159 general patterns, so that a handler might register for the two patterns 3160 "/codesearch" and "codesearch.google.com/" without also taking over 3161 requests for "<a href="http://www.google.com/">http://www.google.com/</a>". 3162 </p> 3163 <p> 3164 ServeMux also takes care of sanitizing the URL request path, 3165 redirecting any request containing . or .. elements to an 3166 equivalent .- and ..-free URL. 3167 </p> 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 <h3 id="NewServeMux">func <a href="https://golang.org/src/net/http/server.go?s=46940:46968#L1582">NewServeMux</a></h3> 3181 <pre>func NewServeMux() *<a href="https://golang.org/pkg/net/http/#ServeMux">ServeMux</a></pre> 3182 <p> 3183 NewServeMux allocates and returns a new ServeMux. 3184 </p> 3185 3186 3187 3188 3189 3190 3191 3192 <h3 id="ServeMux.Handle">func (*ServeMux) <a href="https://golang.org/src/net/http/server.go?s=49985:50045#L1694">Handle</a></h3> 3193 <pre>func (mux *<a href="https://golang.org/pkg/net/http/#ServeMux">ServeMux</a>) Handle(pattern <a href="https://golang.org/pkg/builtin/#string">string</a>, handler <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>)</pre> 3194 <p> 3195 Handle registers the handler for the given pattern. 3196 If a handler already exists for pattern, Handle panics. 3197 </p> 3198 3199 3200 <div id="example_ServeMux_Handle" class="toggle"> 3201 <div class="collapsed"> 3202 <p class="exampleHeading toggleButton">▹ <span class="text">Example</span></p> 3203 </div> 3204 <div class="expanded"> 3205 <p class="exampleHeading toggleButton">▾ <span class="text">Example</span></p> 3206 3207 3208 3209 <p>Code:</p> 3210 <pre class="code"> mux := http.NewServeMux() 3211 mux.Handle("/api/", apiHandler{}) 3212 mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { 3213 <span class="comment">// The "/" pattern matches everything, so we need to check</span> 3214 <span class="comment">// that we're at the root here.</span> 3215 if req.URL.Path != "/" { 3216 http.NotFound(w, req) 3217 return 3218 } 3219 fmt.Fprintf(w, "Welcome to the home page!") 3220 }) 3221 </pre> 3222 3223 3224 </div> 3225 </div> 3226 3227 3228 3229 3230 <h3 id="ServeMux.HandleFunc">func (*ServeMux) <a href="https://golang.org/src/net/http/server.go?s=51148:51235#L1733">HandleFunc</a></h3> 3231 <pre>func (mux *<a href="https://golang.org/pkg/net/http/#ServeMux">ServeMux</a>) HandleFunc(pattern <a href="https://golang.org/pkg/builtin/#string">string</a>, handler func(<a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, *<a href="https://golang.org/pkg/net/http/#Request">Request</a>))</pre> 3232 <p> 3233 HandleFunc registers the handler function for the given pattern. 3234 </p> 3235 3236 3237 3238 3239 3240 3241 <h3 id="ServeMux.Handler">func (*ServeMux) <a href="https://golang.org/src/net/http/server.go?s=48689:48757#L1646">Handler</a></h3> 3242 <pre>func (mux *<a href="https://golang.org/pkg/net/http/#ServeMux">ServeMux</a>) Handler(r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (h <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>, pattern <a href="https://golang.org/pkg/builtin/#string">string</a>)</pre> 3243 <p> 3244 Handler returns the handler to use for the given request, 3245 consulting r.Method, r.Host, and r.URL.Path. It always returns 3246 a non-nil handler. If the path is not in its canonical form, the 3247 handler will be an internally-generated handler that redirects 3248 to the canonical path. 3249 </p> 3250 <p> 3251 Handler also returns the registered pattern that matches the 3252 request or, in the case of internally-generated redirects, 3253 the pattern that will match after following the redirect. 3254 </p> 3255 <p> 3256 If there is no registered handler that applies to the request, 3257 Handler returns a “page not found” handler and an empty pattern. 3258 </p> 3259 3260 3261 3262 3263 3264 3265 <h3 id="ServeMux.ServeHTTP">func (*ServeMux) <a href="https://golang.org/src/net/http/server.go?s=49617:49677#L1680">ServeHTTP</a></h3> 3266 <pre>func (mux *<a href="https://golang.org/pkg/net/http/#ServeMux">ServeMux</a>) ServeHTTP(w <a href="https://golang.org/pkg/net/http/#ResponseWriter">ResponseWriter</a>, r *<a href="https://golang.org/pkg/net/http/#Request">Request</a>)</pre> 3267 <p> 3268 ServeHTTP dispatches the request to the handler whose 3269 pattern most closely matches the request URL. 3270 </p> 3271 3272 3273 3274 3275 3276 3277 3278 3279 <h2 id="Server">type <a href="https://golang.org/src/net/http/server.go?s=52296:53789#L1760">Server</a></h2> 3280 <pre>type Server struct { 3281 Addr <a href="https://golang.org/pkg/builtin/#string">string</a> <span class="comment">// TCP address to listen on, ":http" if empty</span> 3282 Handler <a href="https://golang.org/pkg/net/http/#Handler">Handler</a> <span class="comment">// handler to invoke, http.DefaultServeMux if nil</span> 3283 ReadTimeout <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Duration">Duration</a> <span class="comment">// maximum duration before timing out read of the request</span> 3284 WriteTimeout <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Duration">Duration</a> <span class="comment">// maximum duration before timing out write of the response</span> 3285 MaxHeaderBytes <a href="https://golang.org/pkg/builtin/#int">int</a> <span class="comment">// maximum size of request headers, DefaultMaxHeaderBytes if 0</span> 3286 TLSConfig *<a href="https://golang.org/pkg/crypto/tls/">tls</a>.<a href="https://golang.org/pkg/crypto/tls/#Config">Config</a> <span class="comment">// optional TLS config, used by ListenAndServeTLS</span> 3287 3288 <span class="comment">// TLSNextProto optionally specifies a function to take over</span> 3289 <span class="comment">// ownership of the provided TLS connection when an NPN</span> 3290 <span class="comment">// protocol upgrade has occurred. The map key is the protocol</span> 3291 <span class="comment">// name negotiated. The Handler argument should be used to</span> 3292 <span class="comment">// handle HTTP requests and will initialize the Request's TLS</span> 3293 <span class="comment">// and RemoteAddr if not already set. The connection is</span> 3294 <span class="comment">// automatically closed when the function returns.</span> 3295 TLSNextProto map[<a href="https://golang.org/pkg/builtin/#string">string</a>]func(*<a href="https://golang.org/pkg/net/http/#Server">Server</a>, *<a href="https://golang.org/pkg/crypto/tls/">tls</a>.<a href="https://golang.org/pkg/crypto/tls/#Conn">Conn</a>, <a href="https://golang.org/pkg/net/http/#Handler">Handler</a>) 3296 3297 <span class="comment">// ConnState specifies an optional callback function that is</span> 3298 <span class="comment">// called when a client connection changes state. See the</span> 3299 <span class="comment">// ConnState type and associated constants for details.</span> 3300 ConnState func(<a href="https://golang.org/pkg/net/">net</a>.<a href="https://golang.org/pkg/net/#Conn">Conn</a>, <a href="https://golang.org/pkg/net/http/#ConnState">ConnState</a>) 3301 3302 <span class="comment">// ErrorLog specifies an optional logger for errors accepting</span> 3303 <span class="comment">// connections and unexpected behavior from handlers.</span> 3304 <span class="comment">// If nil, logging goes to os.Stderr via the log package's</span> 3305 <span class="comment">// standard logger.</span> 3306 ErrorLog *<a href="https://golang.org/pkg/log/">log</a>.<a href="https://golang.org/pkg/log/#Logger">Logger</a> 3307 <span class="comment">// contains filtered or unexported fields</span> 3308 }</pre> 3309 <p> 3310 A Server defines parameters for running an HTTP server. 3311 The zero value for Server is a valid configuration. 3312 </p> 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 <h3 id="Server.ListenAndServe">func (*Server) <a href="https://golang.org/src/net/http/server.go?s=55886:55927#L1858">ListenAndServe</a></h3> 3328 <pre>func (srv *<a href="https://golang.org/pkg/net/http/#Server">Server</a>) ListenAndServe() <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 3329 <p> 3330 ListenAndServe listens on the TCP network address srv.Addr and then 3331 calls Serve to handle requests on incoming connections. If 3332 srv.Addr is blank, ":http" is used. 3333 </p> 3334 3335 3336 3337 3338 3339 3340 <h3 id="Server.ListenAndServeTLS">func (*Server) <a href="https://golang.org/src/net/http/server.go?s=60146:60214#L2003">ListenAndServeTLS</a></h3> 3341 <pre>func (srv *<a href="https://golang.org/pkg/net/http/#Server">Server</a>) ListenAndServeTLS(certFile, keyFile <a href="https://golang.org/pkg/builtin/#string">string</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 3342 <p> 3343 ListenAndServeTLS listens on the TCP network address srv.Addr and 3344 then calls Serve to handle requests on incoming TLS connections. 3345 </p> 3346 <p> 3347 Filenames containing a certificate and matching private key for the 3348 server must be provided if the Server's TLSConfig.Certificates is 3349 not populated. If the certificate is signed by a certificate 3350 authority, the certFile should be the concatenation of the server's 3351 certificate, any intermediates, and the CA's certificate. 3352 </p> 3353 <p> 3354 If srv.Addr is blank, ":https" is used. 3355 </p> 3356 3357 3358 3359 3360 3361 3362 <h3 id="Server.Serve">func (*Server) <a href="https://golang.org/src/net/http/server.go?s=56308:56354#L1873">Serve</a></h3> 3363 <pre>func (srv *<a href="https://golang.org/pkg/net/http/#Server">Server</a>) Serve(l <a href="https://golang.org/pkg/net/">net</a>.<a href="https://golang.org/pkg/net/#Listener">Listener</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></pre> 3364 <p> 3365 Serve accepts incoming connections on the Listener l, creating a 3366 new service goroutine for each. The service goroutines read requests and 3367 then call srv.Handler to reply to them. 3368 </p> 3369 3370 3371 3372 3373 3374 3375 <h3 id="Server.SetKeepAlivesEnabled">func (*Server) <a href="https://golang.org/src/net/http/server.go?s=57329:57376#L1912">SetKeepAlivesEnabled</a></h3> 3376 <pre>func (srv *<a href="https://golang.org/pkg/net/http/#Server">Server</a>) SetKeepAlivesEnabled(v <a href="https://golang.org/pkg/builtin/#bool">bool</a>)</pre> 3377 <p> 3378 SetKeepAlivesEnabled controls whether HTTP keep-alives are enabled. 3379 By default, keep-alives are always enabled. Only very 3380 resource-constrained environments or servers in the process of 3381 shutting down should disable them. 3382 </p> 3383 3384 3385 3386 3387 3388 3389 3390 3391 <h2 id="Transport">type <a href="https://golang.org/src/net/http/transport.go?s=1324:4038#L39">Transport</a></h2> 3392 <pre>type Transport struct { 3393 3394 <span class="comment">// Proxy specifies a function to return a proxy for a given</span> 3395 <span class="comment">// Request. If the function returns a non-nil error, the</span> 3396 <span class="comment">// request is aborted with the provided error.</span> 3397 <span class="comment">// If Proxy is nil or returns a nil *URL, no proxy is used.</span> 3398 Proxy func(*<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (*<a href="https://golang.org/pkg/net/url/">url</a>.<a href="https://golang.org/pkg/net/url/#URL">URL</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 3399 3400 <span class="comment">// Dial specifies the dial function for creating unencrypted</span> 3401 <span class="comment">// TCP connections.</span> 3402 <span class="comment">// If Dial is nil, net.Dial is used.</span> 3403 Dial func(network, addr <a href="https://golang.org/pkg/builtin/#string">string</a>) (<a href="https://golang.org/pkg/net/">net</a>.<a href="https://golang.org/pkg/net/#Conn">Conn</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 3404 3405 <span class="comment">// DialTLS specifies an optional dial function for creating</span> 3406 <span class="comment">// TLS connections for non-proxied HTTPS requests.</span> 3407 <span class="comment">//</span> 3408 <span class="comment">// If DialTLS is nil, Dial and TLSClientConfig are used.</span> 3409 <span class="comment">//</span> 3410 <span class="comment">// If DialTLS is set, the Dial hook is not used for HTTPS</span> 3411 <span class="comment">// requests and the TLSClientConfig and TLSHandshakeTimeout</span> 3412 <span class="comment">// are ignored. The returned net.Conn is assumed to already be</span> 3413 <span class="comment">// past the TLS handshake.</span> 3414 DialTLS func(network, addr <a href="https://golang.org/pkg/builtin/#string">string</a>) (<a href="https://golang.org/pkg/net/">net</a>.<a href="https://golang.org/pkg/net/#Conn">Conn</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>) 3415 3416 <span class="comment">// TLSClientConfig specifies the TLS configuration to use with</span> 3417 <span class="comment">// tls.Client. If nil, the default configuration is used.</span> 3418 TLSClientConfig *<a href="https://golang.org/pkg/crypto/tls/">tls</a>.<a href="https://golang.org/pkg/crypto/tls/#Config">Config</a> 3419 3420 <span class="comment">// TLSHandshakeTimeout specifies the maximum amount of time waiting to</span> 3421 <span class="comment">// wait for a TLS handshake. Zero means no timeout.</span> 3422 TLSHandshakeTimeout <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Duration">Duration</a> 3423 3424 <span class="comment">// DisableKeepAlives, if true, prevents re-use of TCP connections</span> 3425 <span class="comment">// between different HTTP requests.</span> 3426 DisableKeepAlives <a href="https://golang.org/pkg/builtin/#bool">bool</a> 3427 3428 <span class="comment">// DisableCompression, if true, prevents the Transport from</span> 3429 <span class="comment">// requesting compression with an "Accept-Encoding: gzip"</span> 3430 <span class="comment">// request header when the Request contains no existing</span> 3431 <span class="comment">// Accept-Encoding value. If the Transport requests gzip on</span> 3432 <span class="comment">// its own and gets a gzipped response, it's transparently</span> 3433 <span class="comment">// decoded in the Response.Body. However, if the user</span> 3434 <span class="comment">// explicitly requested gzip it is not automatically</span> 3435 <span class="comment">// uncompressed.</span> 3436 DisableCompression <a href="https://golang.org/pkg/builtin/#bool">bool</a> 3437 3438 <span class="comment">// MaxIdleConnsPerHost, if non-zero, controls the maximum idle</span> 3439 <span class="comment">// (keep-alive) to keep per-host. If zero,</span> 3440 <span class="comment">// DefaultMaxIdleConnsPerHost is used.</span> 3441 MaxIdleConnsPerHost <a href="https://golang.org/pkg/builtin/#int">int</a> 3442 3443 <span class="comment">// ResponseHeaderTimeout, if non-zero, specifies the amount of</span> 3444 <span class="comment">// time to wait for a server's response headers after fully</span> 3445 <span class="comment">// writing the request (including its body, if any). This</span> 3446 <span class="comment">// time does not include the time to read the response body.</span> 3447 ResponseHeaderTimeout <a href="https://golang.org/pkg/time/">time</a>.<a href="https://golang.org/pkg/time/#Duration">Duration</a> 3448 <span class="comment">// contains filtered or unexported fields</span> 3449 }</pre> 3450 <p> 3451 Transport is an implementation of RoundTripper that supports HTTP, 3452 HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT). 3453 Transport can also cache connections for future re-use. 3454 </p> 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 <h3 id="Transport.CancelRequest">func (*Transport) <a href="https://golang.org/src/net/http/transport.go?s=8881:8928#L269">CancelRequest</a></h3> 3470 <pre>func (t *<a href="https://golang.org/pkg/net/http/#Transport">Transport</a>) CancelRequest(req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>)</pre> 3471 <p> 3472 CancelRequest cancels an in-flight request by closing its connection. 3473 CancelRequest should only be called after RoundTrip has returned. 3474 </p> 3475 3476 3477 3478 3479 3480 3481 <h3 id="Transport.CloseIdleConnections">func (*Transport) <a href="https://golang.org/src/net/http/transport.go?s=8498:8540#L253">CloseIdleConnections</a></h3> 3482 <pre>func (t *<a href="https://golang.org/pkg/net/http/#Transport">Transport</a>) CloseIdleConnections()</pre> 3483 <p> 3484 CloseIdleConnections closes any connections which were previously 3485 connected from previous requests but are now sitting idle in 3486 a "keep-alive" state. It does not interrupt any connections currently 3487 in use. 3488 </p> 3489 3490 3491 3492 3493 3494 3495 <h3 id="Transport.RegisterProtocol">func (*Transport) <a href="https://golang.org/src/net/http/transport.go?s=7866:7934#L234">RegisterProtocol</a></h3> 3496 <pre>func (t *<a href="https://golang.org/pkg/net/http/#Transport">Transport</a>) RegisterProtocol(scheme <a href="https://golang.org/pkg/builtin/#string">string</a>, rt <a href="https://golang.org/pkg/net/http/#RoundTripper">RoundTripper</a>)</pre> 3497 <p> 3498 RegisterProtocol registers a new protocol with scheme. 3499 The Transport will pass requests using the given scheme to rt. 3500 It is rt's responsibility to simulate HTTP request semantics. 3501 </p> 3502 <p> 3503 RegisterProtocol can be used by other packages to provide 3504 implementations of protocol schemes like "ftp" or "file". 3505 </p> 3506 3507 3508 3509 3510 3511 3512 <h3 id="Transport.RoundTrip">func (*Transport) <a href="https://golang.org/src/net/http/transport.go?s=6344:6415#L181">RoundTrip</a></h3> 3513 <pre>func (t *<a href="https://golang.org/pkg/net/http/#Transport">Transport</a>) RoundTrip(req *<a href="https://golang.org/pkg/net/http/#Request">Request</a>) (resp *<a href="https://golang.org/pkg/net/http/#Response">Response</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre> 3514 <p> 3515 RoundTrip implements the RoundTripper interface. 3516 </p> 3517 <p> 3518 For higher-level HTTP client support (such as handling of cookies 3519 and redirects), see Get, Post, and the Client type. 3520 </p> 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 <h2 id="pkg-subdirectories">Subdirectories</h2> 3538 3539 3540 3541 3542 <div class="pkg-dir"> 3543 <table> 3544 <tbody><tr> 3545 <th class="pkg-name">Name</th> 3546 <th class="pkg-synopsis">Synopsis</th> 3547 </tr> 3548 3549 3550 <tr> 3551 <td colspan="2"><a href="https://golang.org/pkg/net/">..</a></td> 3552 </tr> 3553 3554 3555 3556 3557 <tr> 3558 <td class="pkg-name" style="padding-left: 0px;"> 3559 <a href="https://golang.org/pkg/net/http/cgi/">cgi</a> 3560 </td> 3561 <td class="pkg-synopsis"> 3562 Package cgi implements CGI (Common Gateway Interface) as specified in RFC 3875. 3563 </td> 3564 </tr> 3565 3566 3567 3568 <tr> 3569 <td class="pkg-name" style="padding-left: 0px;"> 3570 <a href="https://golang.org/pkg/net/http/cookiejar/">cookiejar</a> 3571 </td> 3572 <td class="pkg-synopsis"> 3573 Package cookiejar implements an in-memory RFC 6265-compliant http.CookieJar. 3574 </td> 3575 </tr> 3576 3577 3578 3579 <tr> 3580 <td class="pkg-name" style="padding-left: 0px;"> 3581 <a href="https://golang.org/pkg/net/http/fcgi/">fcgi</a> 3582 </td> 3583 <td class="pkg-synopsis"> 3584 Package fcgi implements the FastCGI protocol. 3585 </td> 3586 </tr> 3587 3588 3589 3590 <tr> 3591 <td class="pkg-name" style="padding-left: 0px;"> 3592 <a href="https://golang.org/pkg/net/http/httptest/">httptest</a> 3593 </td> 3594 <td class="pkg-synopsis"> 3595 Package httptest provides utilities for HTTP testing. 3596 </td> 3597 </tr> 3598 3599 3600 3601 <tr> 3602 <td class="pkg-name" style="padding-left: 0px;"> 3603 <a href="https://golang.org/pkg/net/http/httputil/">httputil</a> 3604 </td> 3605 <td class="pkg-synopsis"> 3606 Package httputil provides HTTP utility functions, complementing the more common ones in the net/http package. 3607 </td> 3608 </tr> 3609 3610 3611 3612 <tr> 3613 <td class="pkg-name" style="padding-left: 0px;"> 3614 <a href="https://golang.org/pkg/net/http/pprof/">pprof</a> 3615 </td> 3616 <td class="pkg-synopsis"> 3617 Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool. 3618 </td> 3619 </tr> 3620 3621 3622 </tbody></table> 3623 </div> 3624 3625 3626 3627 3628 3629 3630 <div id="footer"> 3631 Build version go1.5.1.<br> 3632 Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>, 3633 the content of this page is licensed under the 3634 Creative Commons Attribution 3.0 License, 3635 and code is licensed under a <a href="https://golang.org/LICENSE">BSD license</a>.<br> 3636 <a href="https://golang.org/doc/tos.html">Terms of Service</a> | 3637 <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a> 3638 </div> 3639 3640 </div><!-- .container --> 3641 </div><!-- #page --> 3642 3643 <!-- TODO(adonovan): load these from <head> using "defer" attribute? --> 3644 <script type="text/javascript" src="./http - The Go Programming Language_files/jquery.min.js"></script> 3645 <script type="text/javascript" src="./http - The Go Programming Language_files/jquery.treeview.js"></script> 3646 <script type="text/javascript" src="./http - The Go Programming Language_files/jquery.treeview.edit.js"></script> 3647 3648 3649 <script type="text/javascript" src="./http - The Go Programming Language_files/playground.js"></script> 3650 3651 <script type="text/javascript" src="./http - The Go Programming Language_files/godocs.js"></script> 3652 3653 <script type="text/javascript"> 3654 (function() { 3655 var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true; 3656 ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js"; 3657 var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s); 3658 })(); 3659 </script> 3660 3661 3662 3663 </body></html>